0

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.

Neel0507
  • 1,094
  • 2
  • 11
  • 18
  • If I understand you correctly... Just retrieve the previous shared preferences when you call thr save changes function. Check if the textview contains a string for example and only then save it to preferences. Now you set new and empty values without checking the previous value. And everything is lost indeed – Riddim Jan 23 '16 at 00:40
  • why can't you try with the sql Lite DB. which will be much useful. – Swaminathan V Jan 23 '16 at 04:20

1 Answers1

0

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.

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • My code snippet is not wrong, I just forgot to do some changes when posting the question, I will edit the question right away, thanks for pointing out. – Neel0507 Jan 23 '16 at 09:16
  • You are now putting multiple variables with the same names in the same scope `String et1 = et1.getText().toString();` – Evin1_ Jan 23 '16 at 18:02
  • Well, the first string et1 (String et1 =) is a new string which is assigned to an autocompletetextview, so second et1 is an autocompletetextview and thus, both et1 are not the same, nor et2,et3,and et4. I will take your edited answer into account and will test it in my application program. Thanks. – Neel0507 Jan 23 '16 at 18:46