2016-10-09 152 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