如何显示以前的EditText用户输入以及最近的EditText用户输入?

时间:2012-04-19 18:32:55

标签: android sharedpreferences

我有一个SharedPreferences,它从一个活动中保存EditText输入,并在另一个活动中显示String值。

当我在EditText字段中输入一个输入并按下(我创建的一个按钮)“Save”(将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();
}

谢谢你的谦逊时光。

2 个答案:

答案 0 :(得分:1)

听起来我正在尝试建立一个列表。请原谅我最初模糊不清的答案,但我保证最终会集中注意力。

我建议整体框架是这样的:

您有一个Activity(可能是您的CustomStoreActivity),它会显示已保存的Store个对象的列表。 Store需要实施ParcelableSerializable(最好是前者)。

在此Activity上有一个New Store按钮(如果您遵循Android设计约定,则可能是操作栏中的&#34; +&#34;按钮)。< / p>

在按下New Store按钮后,您CustomStoreActivity拨打了startActivityForResult(CustomStoreEditActivity.class)

根据您提问的最后question的答案,CustomStoreActivity实施onActivityResult。在这里,您将获得一个结果代码和Intent,其中包含Extras(由intent.getExtras()检索)。

您的CustomStoreEditActivity接受用户输入,如上所述,而不是将其写入SharedPreferences,而是使用该信息创建包含所有信息的新Store对象,并使用setResult(RESULT_OK,intent)其中intentIntent,您已添加Store个额外信息(Parcelable)。

这意味着当CustomStoreEditActivity调用finish()时(您将执行此操作而不是使用意图重新启动CustomStoreActivity,因为CustomStoreActivity已经落后于它)您接到onActivityResult的来电,可以将Store对象从您刚刚传回的Extras中移出Intent

现在到了这一点:

您可以在ListView中使用CustomStoreActivity,并使用展开StoreAdapter的{​​{1}},以及ArrayAdapter<Store>列表中的每个项目将视图添加到包含Store名称的列表中。然后,您只需将新的Store对象添加到传递到Store的数组中,然后在StoreAdapter中调用adapter.notifyDataSetChanged()即可。

如果最终需要在应用程序会话之间保留此数据,则应该处理onActivityResult Store SharedPreferences onPause方法CustomStoreActivity处理onCreate列表的守护者)而不是从各个方向触摸您的共享偏好。然后,您可以根据需要在Activity中再次阅读列表。

我知道解析/摘要很多,但它是一种非常标准的机制,用于在Android中的{{1}}构造之间传递数据以及持久化数据。

如果您对实施有任何疑问,请告诉我,我会看到我能做些什么! :)

答案 1 :(得分:0)

在这里查看我的答案:Show a drop down list of recently entered text when clicks on an android edit box

您真正需要做的就是将偏好模式更改为公开,而不是私有。