Android Studio AsyncTask将Result获取到其他Activity

时间:2017-04-16 12:35:32

标签: android android-asynctask

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

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

DatabaseC529: 构造:

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完成后,结果将显示在TextView中。 但我也希望将值存储在我的字符串“tInt3”中。 我创建了一个按钮(只是为了检查Value是否存储)以在AsyncTask完成后获取tInt3的内容,但它总是显示“null”。 代码按钮:

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

有人可以帮帮我???

1 个答案:

答案 0 :(得分:0)

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

保存价值:

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");