1

I tried so many suggestions: I got to know that we can store the String set in Shared pref, but I have an Integer array list. Now if I'll try Integer Arralist to String arraylist and store in Shared Pref and again do the same, so lengthy process and lots of exception.

Is there any other way?

Make sure I want to use Shared Pref, no other things:

Code:

public Set<String> getAppointmentStatusPosList() {
        return sharedPreferences.getStringSet(APPOINTMENT_STATUS_ID_LIST, null);
    }

    public void setAppointmentStatusPosList(Set<String> vitalId) {
        editor.putStringSet(APPOINTMENT_STATUS_ID_LIST, vitalId);
        editor.apply();
    }

 @Override
    public void selectedIndices(List<Integer> indices) {
        Set<Integer> set = new HashSet<>();
        set.addAll(indices);
        preferenceManager.setAppointmentStatusPosList(set);
       // list1 = indices;
    }
4
  • Plz check stackoverflow.com/questions/7175880/… Commented Jun 19, 2020 at 12:29
  • 2
    You can use GSON to convert it to String then save it in SharedPref. And later you can parse it to back to ArrayList. Commented Jun 19, 2020 at 12:30
  • @ShaluTD checked already, I have arraylist like this List<Integer> indices , not Interger[] Commented Jun 19, 2020 at 12:34
  • @AshutoshSagar Can you post an answer? Commented Jun 19, 2020 at 12:34

2 Answers 2

3

Convert Integer array to string

List<Integer> listInteger = new ArrayList();
listInteger.add(1);
listInteger.add(2);
listInteger.add(3);

// Convert Integer array to String text 
String ss = new Gson().toJson(listInteger);

Save string text to SharedPreference

SharedPreferences prefs = context.getSharedPreferences("com.example.myapplication", Context.MODE_PRIVATE);
        prefs.edit().putString("APPOINTMENT_STATUS_ID_LIST_STRING", ss).apply();

Get string text from SharedPreference

String text = prefs.getString("APPOINTMENT_STATUS_ID_LIST_STRING", "");

Convert the string text to Integer array

// Convert string text to Integer array 
final Type type = new TypeToken<List<? extends Integer>>() {
}.getType();

listInteger = new Gson().fromJson(ss, type);

Note:- You should add below dependency in your app Gradle file.

dependencies {
  implementation "com.squareup.retrofit2:converter-gson:2.3.0"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this...

    int[] list = new int[10];
    //below is test data, add your original data same like below
    for (int i = 0; i < list.length; i++) {
        list[i]=i;
    }

    StringBuilder str = new StringBuilder();
    for (int i = 0; i < list.length; i++) {
        str.append(list[i]).append(",");
    }

    SharedPreferences.Editor editor=preferences.edit();
    editor.putString("string",str.toString());
    editor.commit();



    String savedString = preferences.getString("string", "");
    java.util.StringTokenizer st = new StringTokenizer(savedString, ",");
    int[] savedList = new int[10];
    for (int i = 0; i < 10; i++) {
        savedList[i] = Integer.parseInt(st.nextToken());

        Log.d("====","savedList :  "+savedList[i]);
    }

OR Check this library, its is way to store anything shared preference https://github.com/yehiahd/FastSave-Android

2 Comments

I have arraylist like this List<Integer> indices , not Interger[]
That does not matter. Just use .get(i) instead of [i].

Your Answer

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