I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.
When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored and displayed successfully.
However, I want it so that everytime the user adds another entry in the EditText, it displays underneath the previously added EditText input. Like a list. I understand that both are reading from the SharedPreferences file. How would I go about doing this?
CustomStoreEditActivity - Just storing the user inputs (EditText entries):
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 - where I retrieve the data and display:
public void onResume() {
SharedPreferences prefs = getSharedPreferences("customtime", 0);
String shopName = prefs.getString("shopName", "Empty");
String shopTimeM1 = prefs.getString("Monday1", " ");
String shopTimeM2 = prefs.getString("Monday2", " ");
String shopTimeT1 = prefs.getString("Monday1", " ");
String shopTimeT2 = prefs.getString("Monday2", " ");
String shopTimeW1 = prefs.getString("Monday1", " ");
String shopTimeW2 = prefs.getString("Monday2", " ");
String shopTimeTH1 = prefs.getString("Monday1", " ");
String shopTimeTH2 = prefs.getString("Monday2", " ");
String shopTimeF1 = prefs.getString("Monday1", " ");
String shopTimeF2 = prefs.getString("Monday2", " ");
String shopTimeS1 = prefs.getString("Monday1", " ");
String shopTimeS2 = prefs.getString("Monday2", " ");
String shopTimeSU1 = prefs.getString("Monday1", " ");
String shopTimeSU2 = prefs.getString("Monday2", " ");
String shopComments = prefs.getString("comments", "");
TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);
displayPrefs.setText(
"------------------------------------------------------------" +
"\n\nSHOP NAME: " + shopName +
"\n\nTIMINGS: " + "\n\n Monday: " + shopTimeM1 + " - " + shopTimeM2 +
"\n Tuesday: " + shopTimeT1 + " - " + shopTimeT2 + "\n Wednesday: "
+ shopTimeW1 + " - " + shopTimeW2 + "\n Thursday: " + shopTimeTH1
+ " - " + shopTimeTH2 + "\n Friday: " + shopTimeF1 + " - " + shopTimeF2 +
"\n Saturday: " + shopTimeS1 + " - " + shopTimeS2 + "\n Sunday: " +
shopTimeSU1 + " - " + shopTimeSU2 +
"\n\nCOMMENTS: " + shopComments +
"\n\n------------------------------------------------------------");
super.onResume();
}
Thank you for your humble time.