2017-04-16 69 views
0

我有一個活動,我想從數據庫中顯示我的收到日期。 在TextView中顯示我的結果,這是沒有問題的。但我也想將接收到的數據保存爲字符串值。 我的代碼: 主要活動:Android Studio AsyncTask獲得結果到其他活動

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」中。 在AsyncTask完成後,我創建了一個按鈕(僅用於檢查Value是否存儲)以獲取tInt3的內容,但它始終顯示爲「null」。 代碼Button:

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

有人能幫幫我嗎?

+0

你在哪裏存儲值tInt3? –

+0

[如何獲取OnPostExecute()的結果到主要活動的可能的重複,因爲AsyncTask是一個單獨的類?](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of- onpostexecute-to-main-activity-because-asynctask-is-a) –

+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"); 
相關問題