I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences but I can't figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences isn't the way to go about this? Is there a simpler method?
当前回答
下面的代码是公认的答案,为新手(我)多写几行,例如。说明了如何将set类型对象转换回arrayList,并提供了关于`之前操作的附加指导。putStringSet'和。getstringset '。(谢谢你邪恶)
// shared preferences
private SharedPreferences preferences;
private SharedPreferences.Editor nsuserdefaults;
// setup persistent data
preferences = this.getSharedPreferences("MyPreferences", MainActivity.MODE_PRIVATE);
nsuserdefaults = preferences.edit();
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
//Retrieve followers from sharedPreferences
Set<String> set = preferences.getStringSet("following", null);
if (set == null) {
// lazy instantiate array
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
} else {
// there is data from previous run
arrayOfMemberUrlsUserIsFollowing = new ArrayList<>(set);
}
// convert arraylist to set, and save arrayOfMemberUrlsUserIsFollowing to nsuserdefaults
Set<String> set = new HashSet<String>();
set.addAll(arrayOfMemberUrlsUserIsFollowing);
nsuserdefaults.putStringSet("following", set);
nsuserdefaults.commit();
其他回答
Android sharedpreferences允许您将基本类型(Boolean, Float, Int, Long, String和StringSet,自API11以来可用)作为xml文件保存在内存中。
任何解决方案的关键思想都是将数据转换为这些基本类型之一。
我个人喜欢将my list转换为json格式,然后将其保存为SharedPreferences值中的字符串。
为了使用我的解决方案,你必须添加谷歌gsonlib。
在gradle中只需添加以下依赖项(请使用谷歌的最新版本):
compile 'com.google.code.gson:gson:2.6.2'
保存数据(HttpParam是你的对象):
List<HttpParam> httpParamList = "**get your list**"
String httpParamJSONList = new Gson().toJson(httpParamList);
SharedPreferences prefs = getSharedPreferences(**"your_prefes_key"**, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(**"your_prefes_key"**, httpParamJSONList);
editor.apply();
检索数据(HttpParam是你的对象):
SharedPreferences prefs = getSharedPreferences(**"your_prefes_key"**, Context.MODE_PRIVATE);
String httpParamJSONList = prefs.getString(**"your_prefes_key"**, "");
List<HttpParam> httpParamList =
new Gson().fromJson(httpParamJSONList, new TypeToken<List<HttpParam>>() {
}.getType());
下面的代码是公认的答案,为新手(我)多写几行,例如。说明了如何将set类型对象转换回arrayList,并提供了关于`之前操作的附加指导。putStringSet'和。getstringset '。(谢谢你邪恶)
// shared preferences
private SharedPreferences preferences;
private SharedPreferences.Editor nsuserdefaults;
// setup persistent data
preferences = this.getSharedPreferences("MyPreferences", MainActivity.MODE_PRIVATE);
nsuserdefaults = preferences.edit();
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
//Retrieve followers from sharedPreferences
Set<String> set = preferences.getStringSet("following", null);
if (set == null) {
// lazy instantiate array
arrayOfMemberUrlsUserIsFollowing = new ArrayList<String>();
} else {
// there is data from previous run
arrayOfMemberUrlsUserIsFollowing = new ArrayList<>(set);
}
// convert arraylist to set, and save arrayOfMemberUrlsUserIsFollowing to nsuserdefaults
Set<String> set = new HashSet<String>();
set.addAll(arrayOfMemberUrlsUserIsFollowing);
nsuserdefaults.putStringSet("following", set);
nsuserdefaults.commit();
我能找到的最好的方法是一个使一个2D数组的键,并把数组的自定义项在2-D数组的键,然后检索它通过启动的2D arra。 我不喜欢使用字符串集的想法,因为大多数android用户仍然使用Gingerbread,使用字符串集需要蜂巢。
示例代码: 这里ditor是共享的pref编辑器,rowitem是我的自定义对象。
editor.putString(genrealfeedkey[j][1], Rowitemslist.get(j).getname());
editor.putString(genrealfeedkey[j][2], Rowitemslist.get(j).getdescription());
editor.putString(genrealfeedkey[j][3], Rowitemslist.get(j).getlink());
editor.putString(genrealfeedkey[j][4], Rowitemslist.get(j).getid());
editor.putString(genrealfeedkey[j][5], Rowitemslist.get(j).getmessage());
请在kotlin中使用这两种方法将数据存储在ArrayList中
fun setDataInArrayList(list: ArrayList<ShopReisterRequest>, key: String, context: Context) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val editor = prefs.edit()
val gson = Gson()
val json = gson.toJson(list)
editor.putString(key, json)
editor.apply()
}
fun getDataInArrayList(key: String, context: Context): ArrayList<ShopReisterRequest> {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val gson = Gson()
val json = prefs.getString(key, null)
val type = object : TypeToken<ArrayList<ShopReisterRequest>>() {
}.type
return gson.fromJson<ArrayList<ShopReisterRequest>>(json, type)
}
您可以将其转换为Map Object来存储它,然后在检索SharedPreferences时将值更改回ArrayList。
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?