2016-10-09 207 views
0

我是新來的android和不是100%肯定我有我的問題的術語是正確的,但在這裏。Android更新從不同的活動TextView

我有一個我想要反映設置的TextView的佈局。我面臨的問題是設置頁面(「customsignalsetup」)從我要更新的TextView所在的頁面導航到。在設置頁面上更改選項並使用Finish()關閉時,TextView尚未更新。但是,如果我關閉了頁面TextView,然後再次打開它,它已更新。

這裏是我的代碼:

TextView的更新代碼按壓從菜單,切換到設置頁面

settings = getSharedPreferences("CustomSignalSettings", 0); 
     TextView CustLabel = (TextView)findViewById(R.id.cunitslabel); 
     CustLabel.setText("Custom (" + settings.getString("CustomSignalUnit", "").toString() + ") ="); 

代碼來處理「設置」(在設置頁面上進行任何更改之前)

public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.custom_signal_setup: // Open Settings Page Intent intent = new Intent(PLCActivity.this, customsignalsetup.class); startActivity(intent); return true; case R.id.help: //get help return true; default: return super.onOptionsItemSelected(item); } } 

設置頁碼到U PDATE的TextView

public class customsignalsetup extends AppCompatActivity implements View.OnClickListener { 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.customplcsettingspopup); 

    Button button = (Button) findViewById(R.id.save); 
      button.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
      switch(v.getId()){ 
       case R.id.save: 

//THE BELOW LINE IS SUPPOSED TO SET 'CustLabel' TO THE TEXTVIEW TO BE UPDATED 
        TextView CustLabel = (TextView)findViewById(R.id.cunitslabel); 

        SharedPreferences settings = getSharedPreferences("CustomSignalSettings", 0); 
        SharedPreferences.Editor editor = settings.edit(); 
        editor.putString("CustomSignalUnit",CusUnit.getText().toString()); 
        editor.commit(); 

// THE BELOW LINE IS SUPPOSED TO UPDATE THE TEXTVIEW ON A DIFFERENT PAGE     
CustLabel.setText("Custom (" + settings.getString("CustomSignalUnit", "").toString() + ") ="); 

        finish(); 
      } 
     } 


    } 

我的代碼不引發錯誤,但是TextView的根本不更新。我假設這是因爲加載原始文本視圖的活動尚未結束,只有未來的標籤將被更新?

謝謝

回答

1

把這條線這​​樣,所以你會得到更新的值每次。

@Override 
public void onResume() { 
    super.onResume(); // Always call the superclass method first 
    CustLabel.setText("Custom (" + settings.getString("CustomSignalUnit", "").toString() + ") ="); 
} 
+0

謝謝!非常簡單! – SilverShotBee