我正在尝试在ArrayList
中保存一个SharedPreferences
,但出现错误
java.lang.ClassCastException:无法将java.util.ArrayList强制转换为
com.example.host.myapplication.adapter.DataModel 这是我的代码示例。
DataModel.java
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
}
//...
错误logcat:
java.lang.ClassCastException:无法将java.util.ArrayList强制转换为 com.example.host.myapplication.adapter.DataModel 在com.example.host.myapplication.adapter.CustomCategoryAdapter.getItem(CustomCategoryAdapter.java:45) 在com.example.host.myapplication.adapter.CustomCategoryAdapter.getView(CustomCategoryAdapter.java:69)
答案 0 :(得分:1)
问题出在您的CustomCategoryAdapter
您已将dataSet
声明为ArrayList
,但在您的CustomCategoryAdapter
构造函数中,参数是ArrayList<DataModel>
使用此
ArrayList<DataModel> dataSet
代替此
private ArrayList dataSet;
答案 1 :(得分:1)
我正在尝试在
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());
答案 2 :(得分:0)
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
String json = new Gson().toJson(yourlist);
prefs .edit().putString("testArray",json).commit();
尝试commit();会将其首选项同步写到持久性存储中