【问题标题】:Android Studio AsyncTask get Result to other ActivityAndroid Studio AsyncTask 获取结果到其他 Activity
【发布时间】:2017-04-16 12:35:31
【问题描述】:

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

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

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

谁能帮帮我???

【问题讨论】:

标签: android android-asynctask


【解决方案1】:

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

保存价值:

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

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多