简体   繁体   中英

Android: Storing multiple EditText values to a ListView using SharedPreferences

Scenario: I have four edittexts and three buttons, where, when the user provides some input in one of the edittexts and either clicks on button 1 or button 2, the provided input by the user should be saved in the activity associated with the button 3. In general, button 3 activity stores the "history" of provided inputs by the user.

Currently, I am using sharedpreferences approach which I know that it is not a good approach if you want to store multiple values. I have tried solving this problem using SQLite database and I was not successful with it.

Here is my method called "saveHistory" which is defined in the button 1's and button 2' onclick methods.

private void saveHistory()
    {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        fd=sharedPreferences.edit();

    String et1 = et1.getText().toString();
    String et2 = et2.getText().toString();
    String et3 = et3.getText().toString();
    String et4 = et4.getText().toString();

    fd.putString("et1" , et1);
    fd.putString("et2" , et2);
    fd.putString("et3" , et3);
    fd.putString("et4" , et4);

    fd.apply();
}  

And here is my another class (when clicking on button 3 this class gets called) which retrieves the edittexts values.

public class history extends Activity
{
    public static final String DEFAULT = "No data";
    ListView listView; 
    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor sp; 
    public ArrayAdapter<String> adapter; 

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);

    listView = (ListView) findViewById(R.id.listViewMain);

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sp = sharedPreferences.edit();

    String et1 = sharedPreferences.getString("et1", DEFAULT);
    String et2 = sharedPreferences.getString("et2", DEFAULT);
    String et3 = sharedPreferences.getString("et3", DEFAULT);
    String et4 = sharedPreferences.getString("et4", DEFAULT);

    if (et1.equals(DEFAULT) || et2.equals(DEFAULT) || et3.equals(DEFAULT) || et4.equals(DEFAULT)) 
    {
        Toast.makeText(this, "No history found", Toast.LENGTH_LONG).show();
    }
    else
    {
        String [] values = new String[]{et1,et2,et3,et4};
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        sp.apply();
    }
}
}

The problem I am facing is that I am only seeing one listview with only 1 value being displayed which is obvious as there is no code which does incrementation. When I enter a new value the old value gets overridden. As there are two buttons associated with saveHistory method I am only able to get one value as there is only one listview. So my needs are that I want to store more than one value using sharedpreferences and also clicking on both buttons should save the values and not override each other's value. I know that I am asking too much, but if you can help me find out how to correctly do incrementation with this scenario and with this code, then I would be grateful.

I have gone through many stackoverflow questions and most of them are associated with having one edittext and one button and storing and retrieving those value. However, My application requirements are different, and, as a result, I have posted this question.

You are retrieving the EditTexts values but you are putting something else in your sharedPreferences.

Replace your saveHistory() code with this:

private void saveHistory(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    fd = sharedPreferences.edit();

    String bt = et1.getText().toString();
    String an = et2.getText().toString();
    String cn = et3.getText().toString();
    String in = et4.getText().toString();

    fd.putString("et1" , bt); //These lines were the problem
    fd.putString("et2" , an); //These lines were the problem
    fd.putString("et3" , cn); //These lines were the problem
    fd.putString("et4" , in); //These lines were the problem

    fd.apply();
}

Edit : I'm sorry I think that I understand your question now.

You can store multiple String values with a single SharedPreferences key with PutStringSet() . You need to make the validation of what button is calling the saveHistory() and then create a Set with this info.

Check the link below for an example.

https://stackoverflow.com/a/9054240/2503185

In your case, you would put something like (for each EditText value):

Set<String> faveSet = faves.getStringSet("et1");
faveSet.add(button + ":" + et1_value);
SharedPreferences.Editor editor = faves.edit();
editor.putStringSet("et1", faveSet);
editor.commit();

And then retrieve that and validate each EditText.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM