user4657804
user4657804

Reputation:

save data from arraylist to shared preferences not working

i want to add the data from arraylist to sharedpreferences and retrieve the same. but it is not working. i am getting only one value which is the last value not all values are retrieved from shared preferences. i am getting the values from JSONArray first the added in the arraylist. below is my code to save the data.

 JSONArray arr = new JSONArray(strServerResponse);
 JSONObject jsonObj = arr.getJSONObject(0);
 for (int i = 0; i < arr.length(); i++) {
       pojo = new Pojo();
       JSONObject jobj2 = arr.getJSONObject(i);
       String tipoftheday = jobj2.optString("tipsoftheday");
       ArrayList<String> tii = new ArrayList<String>();
       tii.add(tipoftheday);
       SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
       for (int i1 = 0; i1 < tii.size(); i1++) {
               editor.putString("tipoftheday", TextUtils.join(",", tii));
               editor.commit();
       }
 }

below is the code to retrieve data from shared preferences and using setter to set the d ata retrieved from shared preferences and adding it to list view adapter. i am not getting what is the mistake.

 SharedPreferences prefs=getSharedPreferences("MyPref", MODE_PRIVATE);
        String serialized = prefs.getString("tipoftheday", null);
        List<String> list = Arrays.asList(TextUtils.split(serialized,","));

for(int i=0; i < list.size(); i++){
            String ttt = list.get(i);
            pojo.setTip(ttt);
            tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
            tips.add(pojo);
            listTips.setAdapter(tipsAdapter);
        }

How to save data in shared preferences? I am getting only one value.

Upvotes: 0

Views: 341

Answers (4)

zozelfelfo
zozelfelfo

Reputation: 3776

You are losing your previous values because you are updating the same key inside your SharedPreferences.Editor. What you have to do in order to add more information afterwards is to first retreive your String value, then add your information and finally store all the info inside your SharedPreferences.Editor.

EDIT

I think it should be something like this, I have done it without any IDE or compiler, so it may have some syntax mistakes :)

 SharedPreferences prefs=getSharedPreferences("MyPref", MODE_PRIVATE);
 String serialized = prefs.getString("tipoftheday", null);
 List<String> list;
 if(serialized != null)
     list = Arrays.asList(TextUtils.split(serialized,","));
 list .add(tipoftheday);

 SharedPreferences.Editor editor = prefs.edit();
   for (int i1 = 0; i1 < tii.size(); i1++) {
           editor.putString("tipoftheday", TextUtils.join(",", list));
           editor.apply(); // if you do not need the return value, apply is faster than commit
   }

Hope it helps

EDIT 2

Check also ρяσѕρєя K answer, he has spotted a different mistake you are making :)

Upvotes: 0

Pankaj
Pankaj

Reputation: 8058

You are using two for loop when you are adding values to SharedPreference. Move out the inner loop and your code will be good to get you all values of list. Add some more line to it like below code:

 for (int i = 0; i < arr.length(); i++) {
           pojo = new Pojo();
           JSONObject jobj2 = arr.getJSONObject(i);
           String tipoftheday = jobj2.optString("tipsoftheday");
           ArrayList<String> tii = new ArrayList<String>();
           tii.add(tipoftheday);
  }



 for (int i1 = 0; i1 < tii.size(); i1++) {
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    SharedPreferences prefs=getSharedPreferences("MyPref", MODE_PRIVATE);
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer = prefs.getString("tipoftheday", null).toString();
    editor.putString("tipoftheday", stringBuffer.append(stringBuffer +",", tii.get(i1).toString()));
    editor.commit();
 }

Upvotes: 0

Nikhil Verma
Nikhil Verma

Reputation: 1730

Try this:

It will work:

To store data:

    StringBuilder sb = new StringBuilder();
    JSONArray arr = new JSONArray(strServerResponse);
    JSONObject jsonObj = arr.getJSONObject(0);
    for (int i = 0; i < arr.length(); i++) 
    {
        pojo = new Pojo();
        JSONObject jobj2 = arr.getJSONObject(i);
        String tipoftheday = jobj2.optString("tipsoftheday");
        sb.append(tipoftheday).append(",");
    }
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    editor.putString("tipoftheday", sb).commit();

To retrieve data:

String[] strArr = getSharedPreferences("MyPref", MODE_PRIVATE).getString("tipsoftheday").split(",");

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

how to save data in shared prefernces. i am getting only one value.

Problem is not related to SharedPreferences it is related to data-source which is currently setting inside for-loop.

Set Adapter as:

    for(int i=0; i < list.size(); i++){
          String ttt = list.get(i);
          pojo.setTip(ttt);  
          tips.add(pojo);     
    }

tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
listTips.setAdapter(tipsAdapter);

Upvotes: 1

Related Questions