2012-04-19 58 views
0

我有一個SharedPreferences可以保存一個活動的EditText輸入,並在另一個活動中顯示String值。如何顯示來自EditText用戶輸入的值?

當我在EditText字段中輸入一個輸入並按下(我創建的一個按鈕)「Save」(將EditText提交給編輯器)時,該文件被成功存儲。但是,顯示活動(顯示SharedPreferences中存儲的字符串值)不顯示值。我猜測這是因爲它是onCreate,因此屏幕在運行時會「創建」。如何在用戶立即提交(按下保存)時更新EditText值?

CustomStoreEditActivity - 只需存儲用戶輸入(EditText上的條目):

final Button saveButton = (Button) findViewById(R.id.saveButton); 
    saveButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) { 
      if (saveButton.isClickable()) { 
       SharedPreferences prefs = getSharedPreferences(
         "customtime", 0); 
       // prefs.registerOnSharedPreferenceChangeListener(this); 
       final SharedPreferences.Editor edit = prefs.edit(); 
       EditText shopName = (EditText) findViewById(R.id.shopName); 
       EditText open1 = (EditText) findViewById(R.id.open1); 
       EditText close1 = (EditText) findViewById(R.id.close1); 
       EditText open2 = (EditText) findViewById(R.id.open2); 
       EditText close2 = (EditText) findViewById(R.id.close2); 
       EditText open3 = (EditText) findViewById(R.id.open3); 
       EditText close3 = (EditText) findViewById(R.id.close3); 
       EditText open4 = (EditText) findViewById(R.id.open4); 
       EditText close4 = (EditText) findViewById(R.id.close4); 
       EditText open5 = (EditText) findViewById(R.id.open5); 
       EditText close5 = (EditText) findViewById(R.id.close5); 
       EditText open6 = (EditText) findViewById(R.id.open6); 
       EditText close6 = (EditText) findViewById(R.id.close6); 
       EditText open7 = (EditText) findViewById(R.id.open7); 
       EditText close7 = (EditText) findViewById(R.id.close7); 
       EditText comments = (EditText) findViewById(R.id.comments); 
       edit.putString("shopName", shopName.getText().toString()); 
       edit.putString("Monday1", open1.getText().toString()); 
       edit.putString("Monday2", close1.getText().toString()); 
       edit.putString("Tuesday1", open2.getText().toString()); 
       edit.putString("Tuesday2", close2.getText().toString()); 
       edit.putString("Wednesday1", open3.getText().toString()); 
       edit.putString("Wednesday2", close3.getText().toString()); 
       edit.putString("Thursday1", open4.getText().toString()); 
       edit.putString("Thursday2", close4.getText().toString()); 
       edit.putString("Friday1", open5.getText().toString()); 
       edit.putString("Friday2", close5.getText().toString()); 
       edit.putString("Saturday1", open6.getText().toString()); 
       edit.putString("Saturday2", close6.getText().toString()); 
       edit.putString("Sunday1", open7.getText().toString()); 
       edit.putString("Sunday2", close7.getText().toString()); 
       edit.putString("comments", comments.getText().toString()); 
       edit.commit(); 
       Intent myIntent = new Intent(getBaseContext(), 
         CustomStoreActivity.class); 
       myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(myIntent); 

       Toast.makeText(getBaseContext(), "Opening Hours Saved!", 
         Toast.LENGTH_SHORT).show(); 

      } 

     } 
    }); 

} 

CustomStoreActivity - 在那裏我相信問題在於:

private void displayPreferences(){ 

    SharedPreferences prefs = getSharedPreferences("customtime", 0); 
    String shopName = prefs.getString("shopName", "Empty"); 
    String shopTime1 = prefs.getString("Monday1", " "); 
    String shopTime2 = prefs.getString("Monday2", " "); 
    String shopComments = prefs.getString("comments", ""); 

    TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs); 

    displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments); 

} 

感謝您有充足的時間。

回答

1

我認爲你要找的部分是將你的CustomStoreActivity代碼放在onResume而不是onCreate。無論何時將CustomStoreActivity帶到最前面(包括首次創建時),您將進入onResume的代碼都會發生。

另一種選擇,假定CustomStoreActivity是用來發射包含EditText S,是使用startActivityForResult並將數據傳遞迴結果Intent而不是使用SharedPreferencesActivityHere是一個簡單的例子(儘管請注意setResult呼叫在這個例子中傳遞null,你想在一個Intent通過,如記錄在Android文檔here)。

它也似乎是你想要使用你的第二個Activity像一個帶有可編輯字段的對話框。如果是這樣,另一種選擇是在CustomStoreActivity類中實際使用Dialog類的變體,而不是創建另一個Activity以表現類似。請參閱Android文檔dialogs

+0

這很有效,非常感謝Jon。我不知道我可以從另一個MapActivity重新調用OnResume()。謝謝。 – DesignAndCode 2012-04-19 17:52:37

+0

不客氣;不過,你不應該手動調用'onResume'。我假設你正在查看'CustomStoreActivity'開始,然後啓動編輯器'Activity',在這種情況下,當你在前臺關閉'Activity'時,它應該由Android框架自動完成重新使用來更新文本字段)。 – 2012-04-19 18:26:44

0

我不確定我是否真的瞭解您的問題,但您是否正在嘗試更新您所在行動中不同活動的TextView?在這種情況下,我不認爲這是可以做到的,你應該使用Intents將數據傳遞給活動

相關問題