简体   繁体   中英

not getting data from shared preferences

i have async task where i am getting data from server in JSONArray format. I want to save that data in shared preferences and display it in list view. i am using adapter.

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);
    List<String> listTemp = tii;
    Set<String> temp = new HashSet<String>(listTemp);
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    for (int m=0; m<listTemp.size(); m++){
            temp.addAll(listTemp);
            editor.putStringSet("tipoftheday",temp);
            editor.commit();
    }

i am retrieving the value as in below code.

SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("tipoftheday", null);
 for(String p : set)
 {
       pojo.setTip(p);
       tips.add(pojo);

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

but i am getting only one value. what is wrong in the code. can anyone please help me.

The problem is, when you're storing the data, every time you're creating a new ArrayList .

Here:

ArrayList<String> tii = new ArrayList<String>();

And you're getting the prefs on every iteration of your outer for loop, you don't have to do this. Just get the reference outside your loop and use it when needed.

Here:

SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();

Try to change your code to something like this:

JSONArray arr = new JSONArray(strServerResponse);
JSONObject jsonObj = arr.getJSONObject(0);

SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
ArrayList<String> tii = new ArrayList<String>();


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

    tii.add(tipoftheday);
}

List<String> listTemp = tii;
Set<String> temp = new HashSet<String>(listTemp);

temp.addAll(listTemp);
editor.putStringSet("tipoftheday",temp);
editor.commit();

EDIT: On your "retrieving" part of the code, you forgot to instantiate your Pojo object.

Like:

pojo = new Pojo();

So your last for loop should look something like:

for(String p : set) {
   pojo = new Pojo();
   pojo.setTip(p);
   tips.add(pojo);
}

The problem in your code is that you are setting the Set value in preference every time ie in loop on each element that is why you can only get the last entered value. Change your following code

 Set<String> temp = new HashSet<String>(listTemp);
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    for (int m=0; m<listTemp.size(); m++){
            temp.addAll(listTemp);
            editor.putStringSet("tipoftheday",temp);
            editor.commit();
    }

Replace it with following

Set<String> temp = new HashSet<String>(listTemp);
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    for (int m=0; m<listTemp.size(); m++){
            temp.addAll(listTemp);
    }
editor.putStringSet("tipoftheday",temp);
editor.commit();

And see if its working or not

Edit:-

Change your adding code to

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

    }

List<String> listTemp = tii;
Set<String> temp = new HashSet<String>(listTemp);
SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
temp.addAll(listTemp);
editor.putStringSet("tipoftheday",temp);
editor.commit();

And retrieving code to

SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("tipoftheday", null);
 for(String p : set)
 {
       pojo = new Pojo();
       pojo.setTip(p);
       tips.add(pojo);

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

See if its working or not

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