Question

I want to change the values in an array in shared preferences. Can we make a string-array in shared preferences and use it in our code.? How is it possible as I am entering different values and want user to change values as per user need. I want to use those values in spinner.

No correct solution

OTHER TIPS

Yes you can do that. Refer to Egor's comment in the link here Put and get String array from shared preferences

That is pretty much what you need.

you can not add array in sharedPrefence.. you can only use data type define in this doc. if you put a large number of data in sharedPrefence than that memory required never be free. so you can use Application class .. the memory of application class will be a free when app is force stop..

insert your values like

registrationPreferencesEditor.putInt("arraylength", a.length);
            for(int i=0;i<a.length;i++)
            {
                registrationPreferencesEditor.putInt("a"+(i+1), a[i]);
            }
            registrationPreferencesEditor.commit();

retreive your values

int lengthOfArray = registrationPreferences.getInt("arraylength", 0);
            int b[] = new int[lengthOfArray];
            for(int i=0;i<lengthOfArray;i++)
            {
                b[i] = registrationPreferences.getInt("a"+(i+1), 0);
                Log.e("b"+(i+1),""+b[i]);
            }

before that make sure you have

SharedPreferences registrationPreferences;
SharedPreferences.Editor registrationPreferencesEditor;
int a[]={1,2,3,4,5};
registrationPreferences  = getSharedPreferences("registrationPreferences",
            MODE_WORLD_READABLE);
      registrationPreferencesEditor= registrationPreferences
            .edit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top