0

I just want to retrieve the data that save from sharedpreferences and display it in listview. Currently I just only manage to display the data with only one line in my SecondActivity. What I want is to display those data separately in listview, is there any expert how to solve this thanks in advance

Retrieving data from sharedpref

My shared preferences

My shared preferences

MainActivity to save sharedpref

public void save_append_list_tosharefpref (String data){

    List<String> favorites = new ArrayList<String>();
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings =   PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = settings.edit();

    Map<String, ?> allPrefs = settings.getAll(); //my existing sharedPreference
    Set<String> set = allPrefs.keySet();

    if (allPrefs.toString() =="{}" || allPrefs.toString() =="[]"){
        favorites.add(data);
    }
    else {
        for(String s : set){
            favorites.add(data);
            favorites.add(allPrefs.get(s).toString());
            Log.d(TAG,"bagona"+allPrefs.get(s).toString());
        }

    }
    editor.putString(FAVORITES, favorites.toString());
    editor.commit();
}

SecondActivity

public void view_from_shared_preferences(){
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings =   PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = settings.edit();

    Map<String, ?> allPrefs = settings.getAll(); //your sharedPreference
    Set<String> set = allPrefs.keySet();

    for(String s : set){
        Log.d(TAG,"This is all data "+allPrefs.get(s).toString());

        String rep =allPrefs.get(s).toString();
        String seps = rep.replace("]", "");
        String seps1 = seps.replace("[", "");
        Log.d(TAG,"final data "+seps1);
        arrayAdapter.add(seps1);



    }
}
McLarvel
  • 31
  • 6

2 Answers2

0

Why don't you Use putStringSetinstead of putString


NOTE: required api level 11


Reference of how to use putStringSet is here https://stackoverflow.com/a/7057858/12676247

Jyotish Biswas
  • 674
  • 5
  • 11
0

After getting string with , use

List<String> elephantList = Arrays.asList(str.split(","));

to get ArrayList and use that to set listview

kelvin
  • 1,480
  • 1
  • 14
  • 28