save arraylist in shared preference

后端 未结 5 1251
抹茶落季
抹茶落季 2020-12-06 15:32

I am storing values in ArrayList and pass it to using bundle to next Fragment, and there I set values to my TextView, till here it wor

相关标签:
5条回答
  • 2020-12-06 16:13

    An alternative solution (just thought of it):

    If you have an array called addtos and you want to add the array to shared preferences, since the variable is represented in the dictionary as a String, you could append the array index to the end of that string.

    e.g -

    Storing

    for(int i = 0; i<addtos.size(); i++)
        prefsEditor.putString("addtos"+i, addtos.get(i));
    

    Receiving

    int i = 0;
    while(true){
        if(prefs.getString("addtos"+i, "")!=""){  // or whatever the default dict value is
            // do something with it
            i++;
        }else{break;}
    }
    

    Seems ok to me, if anyone sees a problem with this, let me know.

    Also, no need for ArrayLists

    0 讨论(0)
  • 2020-12-06 16:23

    Use tinydb. check following link you might get some idea. https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

    using tinydb you can store array in local db.

    0 讨论(0)
  • 2020-12-06 16:27

    First download Gson.jar from below link and then add it to libs folder of your project

    http://www.java2s.com/Code/Jar/g/Downloadgson17jar.htm

    then put That ArrayList in the Class and make object of that class then you can save that object to SharedPreferences like below

    public static void save_User_To_Shared_Prefs(Context context, User _USER) {
        SharedPreferences appSharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext());
        SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(_USER);
        prefsEditor.putString("user", json);
        prefsEditor.commit();
    
     }
    

    above code is an example _USER objext contain ArrayList.

    And to read the object have a look at below code

     public static User get_User_From_Shared_Prefs(Context context) {
    
        SharedPreferences appSharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext());
        Gson gson = new Gson();
        String json = appSharedPrefs.getString("user", "");
    
    
        User user = gson.fromJson(json, User.class);
        return user;
    } 
    

    now when you want to get the _USER object call the above function and in result you will have the object and in that you will have the ArrayList

    0 讨论(0)
  • 2020-12-06 16:29

    Complete example of storing and retrieving arraylist in sharedpreference:http://blog.nkdroidsolutions.com/arraylist-in-sharedpreferences/

      public void storeFavorites(Context context, List favorites) {
        // used for store arrayList in json format
                SharedPreferences settings;
                Editor editor;
                settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
                editor = settings.edit();
                Gson gson = new Gson();
                String jsonFavorites = gson.toJson(favorites);
                editor.putString(FAVORITES, jsonFavorites);
                editor.commit();
            }
    
    
            public ArrayList loadFavorites(Context context) {
        // used for retrieving arraylist from json formatted string
                SharedPreferences settings;
                List favorites;
                settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
                if (settings.contains(FAVORITES)) {
                    String jsonFavorites = settings.getString(FAVORITES, null);
                    Gson gson = new Gson();
                    BeanSampleList[] favoriteItems = gson.fromJson(jsonFavorites,BeanSampleList[].class);
                    favorites = Arrays.asList(favoriteItems);
                    favorites = new ArrayList(favorites);
                } else
                    return null;
                return (ArrayList) favorites;
            }
    
    
            public void addFavorite(Context context, BeanSampleList beanSampleList) {
                List favorites = loadFavorites(context);
                if (favorites == null)
                    favorites = new ArrayList();
                favorites.add(beanSampleList);
                storeFavorites(context, favorites);
            }
    
    
            public void removeFavorite(Context context, BeanSampleList beanSampleList) {
                ArrayList favorites = loadFavorites(context);
                if (favorites != null) {
                    favorites.remove(beanSampleList);
                    storeFavorites(context, favorites);
                }
            }
    
    0 讨论(0)
  • 2020-12-06 16:31

    You can use gson:

    To Save Preferences:

     public void save_User_To_Shared_Prefs(Context context, List<User> users) {
            SharedPreferences settings;
            Editor editor;
    
            settings = context.getSharedPreferences(PREFS_NAME,
                    Context.MODE_PRIVATE);
            editor = settings.edit();
    
            Gson gson = new Gson();
            String jsonUsers = gson.toJson(users);
    
            editor.putString(USERS, jsonUsers);
    
            editor.commit();
        }
    

    To get Preferences:

    public ArrayList<User> getUsers(Context context) {
            SharedPreferences settings;
            List<User> users;
    
            settings = context.getSharedPreferences(PREFS_NAME,
                    Context.MODE_PRIVATE);
    
            if (settings.contains(USERS)) {
                String jsonUsers = settings.getString(USERS, null);
                Gson gson = new Gson();
                User[] userItems = gson.fromJson(jsonUsers,
                        User[].class);
    
                users = Arrays.asList(userItems);
                users= new ArrayList<User>(users);
            } else
                return null;
    
            return (ArrayList<User>) users;
        }
    

    To add user:

    public void addUser(Context context, User user) {
            List<Product> favorites = getUsers(context);
            if (users == null)
                users = new ArrayList<User>();
            users.add(user);
            save_User_To_Shared_Prefs(context, users);
        }
    
    0 讨论(0)
提交回复
热议问题