1. Are you ready for the Galaxy S20? Here is everything we know so far!

Save ArrayList to sharedPreferences PLEASE HELP

Discussion in 'Android Development' started by AronAtVW, Oct 5, 2014.

  1. AronAtVW

    AronAtVW Newbie
    Thread Starter

    So I have been scouring forums trying to fix this issue(it's the last thing I have to do to finish my app!). Heres some code in which I've tried the failed attempts are all commented out. Please any info would help tremendously.

    Set ArrayList

    [HIGH] public static void setPrefArr(Context context, String key, ArrayList value)
    {

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = pref.edit();

    ArrayList<String> newArr = new ArrayList<String>();
    newArr.addAll(value);

    // Exception here vvv (java.io.IOException)
    editor.putString(key, ObjectSerializer.serialize(newArr));

    //editor.putString(key, );

    /*
    Gson gson = new Gson();
    String json = new gson.toJson(value);

    editor.putString(key,json);
    editor.commit();

    /*
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = pref.edit();

    for(int i = 0;i < value.size();i++)
    {
    editor.putString(key+i,value.get(i).toString());

    }

    /*
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    Set<String> set = new HashSet<String>();
    set.addAll(value);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putStringSet(key, set);
    editor.commit();
    */

    }[/HIGH]

    Get ArrayList

    [HIGH]public static ArrayList getSharedPrefArr(Context context, String key, int num)
    {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    //ArrayList value = new ArrayList();


    /*
    if(num > 0)
    {
    for (int i = num; i <= value.size(); i++) {
    value.set(i, pref.getInt(key, 0));
    }
    }

    return value;

    /*
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    Set<String> set = new HashSet<String>();
    set = pref.getStringSet(key, null);
    ArrayList value = new ArrayList(set);
    //value.addAll(set);
    //Collections.copy(value,set);
    */

    return value;


    }[/HIGH]

    Call Functions in OnCreate

    [HIGH]Collections.copy(boatN,getSharedPrefArr(getApplicationContext(),boatArrPref,boatArr));
    //boatN = getSharedPrefArr(getApplicationContext(),boatArrPref,boatArr);
    //Collections.copy(boatN, getSharedPrefArr(getApplicationContext(),boatArrPref));
    //boatN.addAll(getSharedPrefArr(getApplicationContext(),boatArrPref));[/HIGH]
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Manaya

    Manaya Member

    Hmm don't know if this is correct but shouldn't it be ArrayList<String> value in the parameter?
     
  3. AronAtVW

    AronAtVW Newbie
    Thread Starter

    I do declare the Array List with the value initially before the function, maybe I should add this to my arguments. One thing I noticed going through forums is people mentioning how serializing the Array List and save it as a string is probably a better idea that putSet because outSet was introduced in a higher api version that might not be supported. Regardless I could try old methods I used again but adding the value of Array List but don't know if that's my biggest problem here
     
  4. AronAtVW

    AronAtVW Newbie
    Thread Starter

    Actually I experimented a little with this

    public static void main(String[] args)
    {
    ArrayList<String> arr = new ArrayList<String>();
    // TODO code application logic here
    arr.add("Hello World ");
    arr.add("How are you? ");

    ArrayList<String> retArr = new ArrayList<String>();
    retArr = arrCheck(arr);

    for(int i = 0; i < retArr.size(); i++)
    {
    System.out.print(retArr.get(i));

    }

    }

    public static ArrayList arrCheck(ArrayList arrGuy)
    {

    ArrayList<String> newArr = new ArrayList<String>();
    newArr.addAll(arrGuy);
    newArr.add("Hey Girl ");

    return newArr;
    }

    }

    I got the same output (Hello world How are you? Hey girl) whether or not in my function I used ArrayList arrGuy or ArrayList<String> arrGuy
     
  5. Manaya

    Manaya Member

    Does your attempt with putStringSet also crash, or it just doesn't work? Try editor.clear() at the start.
     
  6. AronAtVW

    AronAtVW Newbie
    Thread Starter

    No crash, just doesn't seem to work, I think I'm going to try to just write the Array to data file.
     
  7. Manaya

    Manaya Member

    Did you try with editor.clear() before putStringSet?
     
  8. AronAtVW

    AronAtVW Newbie
    Thread Starter

    No I will double check and try it. I am a little worried about string set though because people said not a lot of devices support up to the api it was introduced. Anyways I'll try a little more with shared preferences but if I don't get it I'm moving on to the next step
     
Loading...

Share This Page

Loading...