[英]Trouble with saving arrayList in SharedPreferences
我正在嘗試將ArrayList
保存在SharedPreferences
但出現錯誤
java.lang.ClassCastException:無法將java.util.ArrayList強制轉換為
com.example.host.myapplication.adapter.DataModel這是我的代碼示例。
數據模型
public class DataModel extends ArrayList<String> {
public String name;
public boolean checked;
public DataModel(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
}
PickCategoryActivity
//...
adapter = new CustomCategoryAdapter(getArrayList("CATEGORY"), getApplicationContext());
//...
public void saveArrayList(ArrayList<DataModel> list, String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(key, json);
editor.apply();
}
public ArrayList<DataModel> getArrayList(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = new TypeToken<ArrayList<DataModel>>() {}.getType();
return gson.fromJson(json, type);
}
CustomCategoryAdapter.java
private ArrayList dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
CheckBox checkBox;
}
public CustomCategoryAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.activity_pick_category_row_list, data);
this.dataSet = data;
this.mContext = context;
}
@Override
public DataModel getItem(int position) {
return (DataModel) dataSet.get(position); // i get an error here
}
//...
錯誤日志:
java.lang.ClassCastException:無法將com.example.host.myapplication.adapter.CustomCategoryAdapter.getItem(CustomCategoryAdapter.java:45)上的com.example.host.myapplication.adapter.DataModel強制轉換為java.util.ArrayList。 example.host.myapplication.adapter.CustomCategoryAdapter.getView(CustomCategoryAdapter.java:69)
問題出在您的CustomCategoryAdapter
您已將dataSet
聲明為ArrayList
但在CustomCategoryAdapter
構造函數中,參數為ArrayList<DataModel>
用這個
ArrayList<DataModel> dataSet
代替這個
private ArrayList dataSet;
我正在嘗試將
ArrayList
保存在SharedPreferences
但出現錯誤java.lang.ClassCastException:無法將java.util.ArrayList強制轉換為
com.example.host.myapplication.adapter.DataModel這是我的代碼示例。
那是因為您試圖將DataModel轉換為ArrayList。
您需要像這樣將DataModel更改為簡單的pojo:
public class DataModel {
public String name;
public boolean checked;
public DataModel(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
}
無需擴展ArrayList,因為您的模型用於單個數據項。
然后,您需要在以下位置將saveArrayList和getArrayList方法修改為更具可讀性的方法,如saveCategories
和getCategories()
:
private static final CATEGORIES_KEY = "CATEGORIES";
public void saveCategories(List<DataModel> list){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(CATEGORIES_KEY, json);
editor.apply();
}
public List<DataModel> getCategories(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
List<Model> models;
Gson gson = new Gson();
String json = prefs.getString(CATEGORIES_KEY, null);
Type type = new TypeToken<List<DataModel>>() {}.getType();
try {
models = gson.fromJson(json, type);
} catch (IllegalStateException | JsonSyntaxException exception) {
// You need to catch the error here
Log.d("CONVERT", exception);
}
return models;
}
您可以看到該鍵已更改為常量並在方法內部移動。 這是因為我們需要向方法調用者隱藏細節。 適配器不知道詳細信息。
然后,您可以使用獲取類別列表:
adapter = new CustomCategoryAdapter(getCategories(), getApplicationContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
String json = new Gson().toJson(yourlist);
prefs .edit().putString("testArray",json).commit();
嘗試commit(); 它將其首選項同步寫入持久性存儲
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.