在sharedpreference中存储ArrayList

在sharedpreference中存储ArrayList

本文介绍了在sharedpreference中存储ArrayList go对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的 Arraylist.每次按下按钮时,我都会创建一个对象并将其放置在 ArrayList 中.当我退出应用程序时,我需要保存这个数组列表.当我再次按下按钮时,它应该向保存到共享首选项的现有数组列表中添加一个新对象,

I have an Arraylist of objects.Each time I press a button, I create an object and place it in the ArrayList.I need to save this array list when I exit the app. When I come and again press the button, it should add a new object to the existing array list saved to the Shared Preference,

我无法存储和接收数组列表.我使用了 GSON,但我不太清楚它是如何工作的.

I am not able to store and receive the array list. I used GSON but I am not very clear how it works.

代码片段:

    public static ArrayList <BookMark> bkmarkList = new ArrayList();
static SharedPreferences keyvalues =
    PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
static Editor editor = keyvalues.edit();
    final Button bkmarkButton = (Button) view.findViewById(R.id.mark_button);
            bkmarkButton.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    bkmarkButton.setBackgroundResource(R.drawable.bookmarked);
                    bkmarkButton.setEnabled(false);
                    bkmarkButton.invalidate();
                    String wss = data.mWss;
                    String guid = data.mGuid;
                    String title = data.mTitle;
                    //SharedPreferences keyvalues = activity.getSharedPreferences("bookmark_list",  Context.MODE_PRIVATE);
                    //bkmarkList = (ArrayList<BookMark>) ObjectSerializer.deserialize(keyvalues.getString(bookmark_list, ObjectSerializer.serialize(new ArrayList<BookMark>())));
                    //editor.getStringSet()
                    Gson gson = new Gson();
                    editor.putString("bookmark_list", new Gson().toJson(bkmarkList).toString());
                    if(keyvalues.getString("bookmark_list","")!=null);
                    {
                        ArrayList<BookMark> bkMark = new ArrayList<BookMark>();
                        //bkMark = gson.fromJson("bookmark_list", ArrayList<BookMark>);
                        //String value = keyvalues.getString("bookmark_list","");
                        //keyvalues.getStringSet(gson.fromJson(bkmarkList))
                        //bkmarkList = gson.fromJson(value, "");
                    }
                    bkmarkList.add(new BookMark(wss,guid, title));

                }
            });

推荐答案

GSON非常好用,看这里:Android SharedPreferences 如何保存/存储对象

GSON is very easy to use, look here:How Android SharedPreferences save/store object

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);保存

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);To Save

  Editor prefsEditor = mPrefs.edit();
     Gson gson = new Gson();
     String json = gson.toJson(MyObject);
     prefsEditor.putString("MyObject", json);
     prefsEditor.commit();

检索

  Gson gson = new Gson();
    String json = mPrefs.getString("MyObject", "");
    MyObject obj = gson.fromJson(json, MyObject.class);

这篇关于在sharedpreference中存储ArrayList go对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:05