我的代码我遇到了textview问题,主要活动恢复时没有更新。基本上它有一个textview和一个按钮。程序加载时,它从sharedpreferences文件中读取日期并设置textview文本。当您单击该按钮时,它会启动一个具有日期选择器的新活动,该活动允许您选择要在共享首选项文件中保存的日期。当您单击确定或按更新活动上的后退按钮时,我正在尝试使用新日期更新textview,如果它已更改,但每次运行onresume时它都不会更新textview文本。我已经尝试使主线性布局无效,让它重绘,但不起作用。我尝试使用startactivityforresult启动活动,即使在onactivityresult中处理textview更新也不起作用。
这是主类的代码:
public class ASMain extends Activity implements OnClickListener {
private LinearLayout llMain;
private TextView tvDate;
private Button btnDate;
private SharedPreferences spSettings;
private static final String SETTINGS_FILE = "MySettings";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llMain = (LinearLayout)findViewById(R.id.llMain);
tvDate = (TextView)findViewById(R.id.tvDate);
btnDate = (Button)findViewById(R.id.btnDate);
spSettings = getSharedPreferences(SETTINGS_FILE,0);
tvDate.setText(spSettings.getString("Date", "ERROR"));
btnDate.setOnClickListener(this);
}
@Override
protected void onResume() {
spSettings = getSharedPreferences(SETTINGS_FILE, 0);
tvDate.setText(spSettings.getString("Date", "ERROR"));
super.onResume();
}
@Override
public void onClick(View v) {
if (v.Id == R.id.btnDate)
{
Intent intent = new Intent(this, update.class);
startActivity(intent);
}
}
}
这是update.class代码:
public class update extends Activity implements OnClickListener {
private LinearLayout llUpdate;
private TextView tvLabel;
private DatePicker dpDate;
private Button btnOK;
private SharedPreferences spSettings;
private static final String SETTINGS_FILE = "MySettings";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
llUpdate = (LinearLayout) findViewById(R.id.llUpdate);
tvLabel = (TextView) findViewById(R.id.tvLabel);
dpDate = (DatePicker) findViewById(R.id.dpDate);
btnOK = (Button) findViewById(R.id.btnOK);
spSettings = getSharedPreferences(SETTINGS_FILE, 0);
String sTemp = spSettings.getString("Date", "01/01/2013");
dpDate.updateDate(Integer.parseInt(sTemp.substring(6, 9)), Integer.parseInt(sTemp.substring(0, 1)), Integer.parseInt(sTemp.substring(3, 4)));
btnOK.setOnClickListener(this);
}
@Override
public void onStop() {
SharedPreferences.Editor speSettings = spSettings.edit();
speSettings.putString("Date", String.valueOf(dpDate.getMonth()) + "/" + String.valueOf(dpDate.getDayOfMonth()) + "/" + String.valueOf(dpDate.getYear()));
speSettings.commit();
super.onStop();
}
@Override
public void onClick(View v) {
if (v.Id = R.id.btnOK)
{
this.finish();
}
}
}
如果再次单击该按钮,textview将会更新,但是一旦我返回主活动,我在启动的意图中所做的任何更改都不会显示。
答案 0 :(得分:1)
在您的onResume()
中进行更改,如下所示..
@Override
protected void onResume() {
spSettings = getSharedPreferences(SETTINGS_FILE, 0);
tvDate.setText(spSettings.getString("Date", "ERROR"));
super.onResume();
}
答案 1 :(得分:0)
从Activity
开始startActivityForResult()
返回后,您需要在onActivityResult()
而不是onResume()
处理结果。
请遵循以下示例:http://developer.android.com/training/basics/intents/result.html