1

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.

4

2 回答 2

1

It sounds to me like you are trying to build a list. Pardon my initially nebulous answer but I promise it will eventually focus in on a point.

What I would recommend for the overall framework is this:

You have an Activity (presumably your CustomStoreActivity) which displays a list of saved Store objects. Store will need to implement Parcelable or Serializable (preferably the former).

On this Activity there is a New Store button (if you are following Android design convention, this is likely as a "+" button in the Action Bar).

On pressing the New Store button, your CustomStoreActivity calls startActivityForResult(CustomStoreEditActivity.class).

Your CustomStoreActivity implements onActivityResult as per the answer on the last question you asked. Here, you will get a result code and an Intent which contains Extras (to be retrieved by intent.getExtras()).

Your CustomStoreEditActivity takes user input, as above, and instead of writing it to SharedPreferences it uses the information to create a new Store object which contains all of the information, and uses setResult(RESULT_OK,intent) where intent is the Intent to which you have added the Store extra (as a Parcelable).

This means that when CustomStoreEditActivity calls finish() (which you will do instead of using an intent to re-launch your CustomStoreActivity because the CustomStoreActivity is already just behind it) you'll get your call to onActivityResult and can pull the Store object out of the Extras from the Intent you just passed back.

And now to the point:

You can use a ListView in your CustomStoreActivity, and use a StoreAdapter that extends ArrayAdapter<Store>, and for each item in the list of Stores adds a view to the list containing that Store's name. Then you'll just add your new Store object to the array you passed into your StoreAdapter, and call adapter.notifyDataSetChanged() in onActivityResult and that should take care of it.

If ultimately you need to persist this data between application sessions, you should handle writing the Store objects to the SharedPreferences in the onPause method of CustomStoreActivity (it being the keeper of the list) rather than touching your shared preferences from every direction. You can then read in the list again in onCreate should you need to.

I know it's a lot to parse through/digest, but it's a pretty standard mechanism for passing data between Activity constructs in Android and for persisting data.

Let me know if you have any questions on the implementation and I'll see what I can do! :)

于 2012-04-19T21:18:31.047 回答
0

Take a look at my answer here: Show a drop down list of recently entered text when clicks on an android edit box

All you would really need to do is change the preference mode to public, not private.

于 2013-03-13T13:28:46.197 回答