androidgsonsharedpreferencesandroid-sharedpreferences

Unique data isn't fetched when using SharedPreferences


I have an Activity on which users register and on the second Activity, all the users who have registered till now, the info will be displayed.

I know this can be done easily using databases but I want to use SharedPreferences. I am using Gson to store/retrieve data. I have a class Student with fields id(rollno),name.

I have maintained a counter for the number of users and this counter will be used for the retrieving side.

Code which saves data to shared preference:

 SharedPreferences preferences = 
    getSharedPreferences("mypref",MODE_PRIVATE);

        SharedPreferences.Editor prefsEditor=preferences.edit();
        Gson gson=new Gson();

        if(preferences.contains("counter")){
            counter=preferences.getInt("counter",0);
            prefsEditor.putInt("counter",++counter);

        }
        else{
            counter=0;
            prefsEditor.putInt("counter",++counter);
        }

        String json = gson.toJson(student);
        prefsEditor.putString("details"+counter,json);
        prefsEditor.commit();

        startActivity(intent);
}

Code for retrieving the SharedPreference:

  SharedPreferences mPrefs = 
                    getSharedPreferences("mypref",MODE_PRIVATE);
    int howMany=mPrefs.getInt("counter",0);
    Gson gson = new Gson();
    for(int i=1;i<=howMany;i++) {
        String json = mPrefs.getString("details" + howMany, "");
        Student student = gson.fromJson(json, Student.class);
        Log.e("tag","loop number"+i);
        Log.e("Tag", student.getName());
    }

When the application starts, my first user's name was "Pra" and the submit button was clicked on the first Activity so that this was stored in SharedPreferences.

The second user's name was "Suv" and this value too was stored in SharedPreferences. The issue now is that the value of the name of the first user has also changed. I am not getting it.

I have maintained the counter so that every object has unique entry in SharedPreferences but the Logs show this:

05-23 10:45:48.208 18409-18409/com.example.com.applicationstudent 
E/tag: counter: 2
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/tag: loop number1
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/Tag: suv
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/tag: loop number2
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/Tag: suv

Again, I don't want to use a database, I want to use SharedPreferences.


Solution

  • Please try correcting the read logic,

     for(int i=1;i<=howMany;i++) {
        String json = mPrefs.getString("details" + howMany, "");
        Student student = gson.fromJson(json, Student.class);
        Log.e("tag","loop number"+i);
        Log.e("Tag", student.getName());
    }
    

    use "i" in mPrefs.getString instead of "howMany". As the value of "howMany" will be constant throughout the loop, same record will be fetched.

     for(int i=1;i<=howMany;i++) {
        String json = mPrefs.getString("details" + i, "");
        Student student = gson.fromJson(json, Student.class);
        Log.e("tag","loop number"+i);
        Log.e("Tag", student.getName());
    }