I have array values of up[]={0,0,0,0,0} and view="adult" thesetwo value i want to store and retrieve in from sharedpreference how to do that...
2
-
Have a look at : stackoverflow.com/questions/3249996/…Kartik Domadiya– Kartik Domadiya2011-10-05 04:17:27 +00:00Commented Oct 5, 2011 at 4:17
-
Can you give us a little information on what exactly it is your storing here? SharedPreferences might not actually be the best thing to use, or perhaps using SharedPreferences differently might be a better solution.Kurtis Nusbaum– Kurtis Nusbaum2011-10-05 16:23:42 +00:00Commented Oct 5, 2011 at 16:23
Add a comment
|
3 Answers
Assuming that you have a list of preferences for you app called MY_PREFS, I'd do this:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",up.size());
for(int i=0; i<up.size(); i++){
editor.putInt("up"+String.valueOf(i), up[i]);
}
editor.putString("view", "adult");
To retrieve them do:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
int arraySize = settings.getInt("arrayLength");
int up[] = new int[arraySize];
for(int i=0; i<arraySize; i++){
up[i] = settings.getInt("up"+String.valueOf(i));
}
String view = settings.getString("view");
7 Comments
user828948
in method public void SetCaseInfo(String type,String up[]) do i have to pass like this or public void SetCaseInfo(String type,new ArrayList())
steven
@user828948 Unless you are wanting an ArrayList just pass in your array like the top one you have posted.
user828948
i have to pass array or arraylist to store data
steven
public void SetCaseInfo(String type,String up[]) should work fine.user828948
but the pbm is when i call setCaseInfo(type,teeth[i]) so this line i have to for loop.so this method will be called 32 times
|
you can do by simple way, like this
SharedPreferences settings = getSharedPreferences("pref_name", 0);
SharedPreferences.Editor editor = settings.edit();
String values="";
for(int i=0; i<yourArray.length; i++){
values+=","+Integer.toString(yourArray[i]);
}
editer.putString("array",values);
editor.putString("view", "adult");
To get those values,
SharedPreferences settings = getSharedPreferences("pref_name", 0);
String[] strArray=settings.getString("array").split(",");
int[] yourArray=new int[strArray.length];
for(int i=0;i<strArray.length;i++){
yourArray[i]=Integer.toParseInt(strArray[i]);
}
3 Comments
user828948
while storing in method i have to pass one string and arraylist object right
user828948
in loop u r taking yourarray.size,nextline yourArray[i],yourarray is array or arraylist
user828948
but the pbm is when i call setCaseInfo(type,teeth[i]) so this line i have to call in for loop.so this method will be called 32 times
Well as far as I have seen you can't directly store an array in shared preferences. You can however use a for loop to save an int with an increasing name and have it call it back the same way when you need it and store it into another array.
for(int i=0; i<numInYourArray; i++){
editor.putInt("up"+i, up[i]);
}
If you aren't sure on how to use Shared Preferences in general look here
1 Comment
user828948
is it like this public void SetCaseInfo(String type,String up[]){ SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString(TYPE,type); for(int i=0; i<up.length; i++){ editor.putInt("up"+i, up[i]); } }