Android Studio AsyncTask 获取其他活动的结果



我有一个活动,我想在其中显示我从数据库中收到的日期。在文本视图中显示我的结果,这没问题。但我也想将收到的数据保存在字符串值中。我的代码:主要活动:

db3 = new DatabaseC529(C529chooseActivity.this,tvC529Cthirdoccupied,tInt3);
db3.execute(methode, date, timeId3);

数据库C529:构造 函数:

public DatabaseC529(Activity activity, TextView textView, String myValue){  
        this.activity = activity;
        this.ctx = activity;
        this.textView = textView;
        this.myValue = myValue;}

protected void onPostExecute(String result) {super.onPostExecute(result);
this.textView.setText(result);
this.myValue = result;
 }

AsyncTask 完成后,结果将显示在文本视图中。但我也想将值存储在我的字符串"tInt3"中。我创建了一个按钮(只是为了检查是否存储了值(来在 AsyncTask 完成后获取 tInt3 的内容,但它总是显示"null"。代码按钮:

public void button1 (View view){if(view.getId() == R.id.button1){
        Toast.makeText(this, " " +tInt3, Toast.LENGTH_SHORT).show();
    }

有人可以帮我吗???

您可以使用共享首选项来存储字符串数据,然后使用它。

保存值:

SharedPreferences.Editor editor = getSharedPreferences(yourpref, MODE_PRIVATE).edit();
 editor.putString("tInt3", Result);
 editor.apply();

获取值:

SharedPreferences prefs = getSharedPreferences(yourpref, MODE_PRIVATE);
 String value= prefs.getString("tInt3", "null");

最新更新