问题描述
我有对象的ArrayList。
每次我preSS一个按钮,我创建一个对象,并将其放置在ArrayList中。
我需要保存这个数组列表,当我退出该应用程序。当我来到又一次preSS的按钮,应该添加一个新的对象来保存到共享preference现有数组列表,
我不能够存储和接收数组列表。我用GSON但我不是很清楚它是如何工作的。
code段:
公共静态的ArrayList<书签及GT; bkmarkList =新的ArrayList();
静态共享preferences键值=
preferenceManager.getDefaultShared preferences(activity.getApplicationContext());
静态编辑器编辑= keyvalues.edit();
最终按钮bkmarkButton =(按钮)view.findViewById(R.id.mark_button);
bkmarkButton.setOnClickListener(新OnClickListener()
{ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
bkmarkButton.setBackgroundResource(R.drawable.bookmarked);
bkmarkButton.setEnabled(假);
bkmarkButton.invalidate();
字符串WSS = data.mWss;
字符串GUID = data.mGuid;
字符串title = data.mTitle;
//共享preferences键值= activity.getShared preferences(bookmark_list,Context.MODE_PRIVATE);
// bkmarkList =(ArrayList的<书签及GT;)ObjectSerializer.deserialize(keyvalues.getString(bookmark_list,ObjectSerializer.serialize(新的ArrayList<书签及GT;())));
//editor.getStringSet()
GSON GSON =新GSON();
editor.putString(bookmark_list,新GSON()的toJSON(bkmarkList)的ToString());
如果(keyvalues.getString(bookmark_list,)!= NULL);
{
ArrayList的<书签及GT; bkMark =新的ArrayList<书签及GT;();
// bkMark = gson.fromJson(bookmark_list的ArrayList<书签及GT;);
//字符串值= keyvalues.getString(bookmark_list,);
//keyvalues.getStringSet(gson.fromJson(bkmarkList))
// bkmarkList = gson.fromJson(价值,);
}
bkmarkList.add(新的书签(WSS,GUID,标题)); }
});
GSON是非常容易使用,请看这里:
<一href=\"http://stackoverflow.com/questions/7145606/how-android-shared$p$pferences-save-store-object\">How Android的共享preferences保存/存储对象
共享preferences米preFS = GET preferences(MODE_PRIVATE);
要保存
编辑器prefsEditor = M prefs.edit();
GSON GSON =新GSON();
JSON字符串= gson.toJson(myObject的);
prefsEditor.putString(MyObject来,JSON);
prefsEditor.commit();
中检索
GSON GSON =新GSON();
JSON字符串= M prefs.getString(MyObject来,);
为MyObject OBJ = gson.fromJson(JSON,MyObject.class);
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,
I am not able to store and receive the array list. I used GSON but I am not very clear how it works.
Code Snippet:
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 is very easy to use, look here:How Android SharedPreferences save/store object
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();
To Retreive
Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);
这篇关于存储ArrayList中去共享preference对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!