0

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/… Commented 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. Commented Oct 5, 2011 at 16:23

3 Answers 3

1

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");
Sign up to request clarification or add additional context in comments.

7 Comments

in method public void SetCaseInfo(String type,String up[]) do i have to pass like this or public void SetCaseInfo(String type,new ArrayList())
@user828948 Unless you are wanting an ArrayList just pass in your array like the top one you have posted.
i have to pass array or arraylist to store data
public void SetCaseInfo(String type,String up[]) should work fine.
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
|
1

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

while storing in method i have to pass one string and arraylist object right
in loop u r taking yourarray.size,nextline yourArray[i],yourarray is array or arraylist
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
0

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

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]); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.