I my code I am having issues with the textview , it is not updating when the main activity resumes. Basically it has a textview and a button. When the program loads it reads the date from a sharedpreferences file and sets the textview text. when you click the button, it launches a new activity that has a datepicker that allows you to choose the date to save in the sharedpreferences file. when you click ok or press the back button on the update activity, I am trying to get the textview to update with the new date if it was changed but everytime it runs the onresume it does not update the textview text. I have tried invalidating the main linearlayout to have it redraw and that does not work. I have tried starting the activity with startactivityforresult and that doesnt work even when processing the textview update in onactivityresult.
This is the code for the main class:
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);
}
}
}
This is the update.class code:
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();
}
}
}
The textview will update if I click the button again but any changes that I make in the intent I started do not show up once I return to the main activity.