あるアクティビティからの EditText 入力を保存し、別のアクティビティで文字列値を表示する SharedPreferences があります。
EditText フィールドに入力を入力し、(私が作成したボタン) [保存] (EditText をエディターにコミットする) を押すと、ファイルが保存され、正常に表示されます。
ただし、ユーザーが EditText に別のエントリを追加するたびに、以前に追加した EditText 入力の下に表示されるようにしたいと考えています。リストのように。どちらも SharedPreferences ファイルから読み取っていることを理解しています。どうすればこれを行うことができますか?
CustomStoreEditActivity - ユーザー入力 (EditText エントリ) を保存するだけです。
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 - データを取得して表示する場所:
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();
}
謙虚な時間をありがとう。