简体   繁体   中英

How to Store Integer Arraylist in Shared Pref and retrieve the same in Android/Java?

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

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"
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM