Android Studio:使用 SharedPreferences 保存和加载 ArrayList
Android Studio: save and load an ArrayList using SharedPreferences
正如问题所问。由于我使用的是 ArrayLists 以及片段,因此我在使用正在制作的应用程序保存和加载共享首选项时遇到了一些问题。
我已经在下面发布了答案,希望它能帮助任何可能像我一样被绊倒的人。
这是一个快速模板,用于将列表保存到您正在制作的任何应用程序中的共享首选项(这和 loadList 都在您的 OnCreate 方法之外:
private void saveList(ArrayList<YourObject> yourList, String yourKey) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(yourList);
prefsEditor.putString(yourKey, json);
prefsEditor.apply();
}
它说 ...(this.getApplicationContext());
,对于 Fragments,您可以使用 ...(getActivity());
此外,请务必在最后使用 .apply();
而不是 .commit();
,因为应用程序在后台运行。
此保存方法也适用于任何对象,而不仅仅是列表。您所要做的就是代替 ...saveList(ArrayList<YourObjectItems> yourList,...
,只需输入不同的对象,例如 ...saveList(String yourString,...
--------------- ------------------------- ------------------------- ------------------------- ------------------------- --
这是 loadList 方法的模板:
private ArrayList<YourObject> loadList(String yourKey) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = prefs.getString(yourKey, null);
Type type = new TypeToken<ArrayList<YourObject>>() {}.getType();
return gson.fromJson(json, type);
}
如果你想加载列表以外的东西,比如说一个字符串值,在倒数第二行你会放:Type type = new TypeToken<String>() {}.getType();
重要!!!
为了使用 Gson()
,您必须输入
implementation 'com.google.code.gson:gson:2.8.2'
在 Gradle 脚本中的 "dependencies" 下 --> build.gradle (Module: app)
.
我相信这应该涵盖所有内容。评论我应该做的任何编辑。希望这可以帮助一些人。另外,仅供参考,我在使用 Android 7.0 构建 API 24 版本的应用程序上使用它。我不知道这是否适用于旧版本,但新版本应该能够处理它。
正如问题所问。由于我使用的是 ArrayLists 以及片段,因此我在使用正在制作的应用程序保存和加载共享首选项时遇到了一些问题。
我已经在下面发布了答案,希望它能帮助任何可能像我一样被绊倒的人。
这是一个快速模板,用于将列表保存到您正在制作的任何应用程序中的共享首选项(这和 loadList 都在您的 OnCreate 方法之外:
private void saveList(ArrayList<YourObject> yourList, String yourKey) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(yourList);
prefsEditor.putString(yourKey, json);
prefsEditor.apply();
}
它说 ...(this.getApplicationContext());
,对于 Fragments,您可以使用 ...(getActivity());
此外,请务必在最后使用 .apply();
而不是 .commit();
,因为应用程序在后台运行。
此保存方法也适用于任何对象,而不仅仅是列表。您所要做的就是代替 ...saveList(ArrayList<YourObjectItems> yourList,...
,只需输入不同的对象,例如 ...saveList(String yourString,...
--------------- ------------------------- ------------------------- ------------------------- ------------------------- --
这是 loadList 方法的模板:
private ArrayList<YourObject> loadList(String yourKey) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = prefs.getString(yourKey, null);
Type type = new TypeToken<ArrayList<YourObject>>() {}.getType();
return gson.fromJson(json, type);
}
如果你想加载列表以外的东西,比如说一个字符串值,在倒数第二行你会放:Type type = new TypeToken<String>() {}.getType();
重要!!!
为了使用 Gson()
,您必须输入
implementation 'com.google.code.gson:gson:2.8.2'
在 Gradle 脚本中的 "dependencies" 下 --> build.gradle (Module: app)
.
我相信这应该涵盖所有内容。评论我应该做的任何编辑。希望这可以帮助一些人。另外,仅供参考,我在使用 Android 7.0 构建 API 24 版本的应用程序上使用它。我不知道这是否适用于旧版本,但新版本应该能够处理它。