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?
当前回答
该方法用于存储/保存数组列表:-
public static void saveSharedPreferencesLogList(Context context, List<String> collageList) {
SharedPreferences mPrefs = context.getSharedPreferences("PhotoCollage", context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(collageList);
prefsEditor.putString("myJson", json);
prefsEditor.commit();
}
该方法用于检索数组列表:-
public static List<String> loadSharedPreferencesLogList(Context context) {
List<String> savedCollage = new ArrayList<String>();
SharedPreferences mPrefs = context.getSharedPreferences("PhotoCollage", context.MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("myJson", "");
if (json.isEmpty()) {
savedCollage = new ArrayList<String>();
} else {
Type type = new TypeToken<List<String>>() {
}.getType();
savedCollage = gson.fromJson(json, type);
}
return savedCollage;
}
其他回答
为什么不把数组列表放在应用程序类上呢?它只有在应用被杀死时才会被销毁,所以,只要应用可用,它就会一直存在。
您还可以将数组列表转换为String并优先保存
private String convertToString(ArrayList<String> list) {
StringBuilder sb = new StringBuilder();
String delim = "";
for (String s : list)
{
sb.append(delim);
sb.append(s);;
delim = ",";
}
return sb.toString();
}
private ArrayList<String> convertToArray(String string) {
ArrayList<String> list = new ArrayList<String>(Arrays.asList(string.split(",")));
return list;
}
您可以使用convertToString方法将数组列表转换为字符串后保存它,并使用convertToArray方法检索字符串并将其转换为数组
在API 11之后,你可以直接保存设置到SharedPreferences !!:)
public class VcareSharedPreference {
private static VcareSharedPreference sharePref = new VcareSharedPreference();
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
private VcareSharedPreference() {
}
public static VcareSharedPreference getInstance(Context context) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
return sharePref;
}
public void save(String KEY, String text) {
editor.putString(KEY, text);
editor.commit();
}
public String getValue(String PREFKEY) {
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
text = sharedPreferences.getString(PREFKEY, null);
return text;
}
public void removeValue(String KEY) {
editor.remove(KEY);
editor.commit();
}
public void clearAll() {
editor.clear();
editor.commit();
}
public void saveArrayList(String key, ArrayList<ModelWelcome> modelCourses) {
Gson gson = new Gson();
String json = gson.toJson(modelCourses);
editor.putString(key, json);
editor.apply();
}
public ArrayList<ModelWelcome> getArray(String key) {
Gson gson = new Gson();
String json = sharedPreferences.getString(key, null);
Type type = new TypeToken<ArrayList<ModelWelcome>>() {
}.getType();
return gson.fromJson(json, type);}}
您可以将其转换为Map Object来存储它,然后在检索SharedPreferences时将值更改回ArrayList。
我使用相同的方式保存和检索字符串,但这里我使用HashSet作为数组列表的中介
使用HashSet保存arrayList到SharedPreferences:
1-我们创建SharedPreferences变量(在数组发生变化的地方)
2 -我们将数组列表转换为HashSet
3 -然后放入stringSet并应用
4 -你在HashSet中获取stringset并重新创建ArrayList来设置HashSet。
public class MainActivity extends AppCompatActivity {
ArrayList<String> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = this.getSharedPreferences("com.example.nec.myapplication", Context.MODE_PRIVATE);
HashSet<String> set = new HashSet(arrayList);
prefs.edit().putStringSet("names", set).apply();
set = (HashSet<String>) prefs.getStringSet("names", null);
arrayList = new ArrayList(set);
Log.i("array list", arrayList.toString());
}
}
推荐文章
- Manifest合并失败:uses-sdk:minSdkVersion 14
- 为什么Android工作室说“等待调试器”如果我不调试?
- 如何检查我的EditText字段是否为空?
- Android从图库中选择图像
- 后台任务,进度对话框,方向改变-有任何100%工作的解决方案吗?
- Android:垂直对齐多行EditText(文本区域)
- Android无尽列表
- Android room persistent: AppDatabase_Impl不存在
- 错误:执行失败的任务':app:compileDebugKotlin'。>编译错误。详细信息请参见日志
- 在Android中使用URI生成器或使用变量创建URL
- 缩放图像以填充ImageView宽度并保持纵横比
- 列表视图的自定义适配器
- 在Android中设置TextView span的颜色
- 如何以编程方式在RelativeLayout中布局视图?
- Android Facebook集成无效键散列