• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Save ArrayList to sharedPreferences PLEASE HELP

AronAtVW

Newbie
Aug 7, 2014
10
0
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]
 
Hmm don't know if this is correct but shouldn't it be ArrayList<String> value in the parameter?
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
 
Upvote 0
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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones