在Java/Android中保存和检索自定义对象列表
我是泛型领域的新手,正在尝试编写一个实用程序类,该类将获取一个对象列表并将其持久化到存储区,然后将其检索回来 以下是我为保存列表而编写的内容:在Java/Android中保存和检索自定义对象列表,android,generics,storage,Android,Generics,Storage,我是泛型领域的新手,正在尝试编写一个实用程序类,该类将获取一个对象列表并将其持久化到存储区,然后将其检索回来 以下是我为保存列表而编写的内容: public static void saveListToStore(Context ctx, String fileName, list<Object> listToStore) throws IOException { String elemValue = ""; Gson gson = new
public static void saveListToStore(Context ctx, String fileName, list<Object> listToStore) throws IOException
{
String elemValue = "";
Gson gson = new Gson();
try {
FileOutputStream fileOutputStream = ctx.openFileOutput(fileName, ctx.MODE_PRIVATE);
elemValue= gson.toJson(listToStore);
fileOutputStream.write(elemValue.getBytes());
objectOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void saveListToStore(上下文ctx、字符串文件名、列表listToStore)引发IOException
{
字符串elemValue=“”;
Gson Gson=新的Gson();
试一试{
FileOutputStream FileOutputStream=ctx.openFileOutput(文件名,ctx.MODE\u PRIVATE);
elemValue=gson.toJson(listToStore);
write(elemValue.getBytes());
objectOutputStream.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
但是,当我尝试检索时,我将不知道列表中存在的对象的类型,并且无法重新构建它。我不想进行类型比较,因为我想保存任何类型的自定义类,而且列表可能会很大
我想从内容本身推断类型。我正在考虑将类型保存为第一行,然后保存数据。所以在retrieve上,我可以先获取类型,然后对对象进行类型转换。但是,还有其他更干净的方法来实现这一点吗?您的对象应该实现可序列化,下面的代码可以帮助您读写
public static void readListToStore(Context ctx, String fileName, List<Object> listToStore) throws IOException {
SharedPreferences storeDataPref = ctx.getSharedPreferences("UR_KEY", Context.MODE_PRIVATE);
String elemValue = storeDataPref.getString("UR_NAME", null);
if (elemValue != null) {
Type listType = new TypeToken<ArrayList<Object>>() {
}.getType();
listToStore = new Gson().fromJson(elemValue, listType);
}
}
public static void saveListToStore(Context ctx, String fileName, List<Object> listToStore) throws IOException {
String elemValue = "";
Gson gson = new Gson();
try {
FileOutputStream fileOutputStream = ctx.openFileOutput(fileName, ctx.MODE_PRIVATE);
elemValue = gson.toJson(listToStore);
SharedPreferences storeDataPref = ctx.getSharedPreferences("UR_KEY", Context.MODE_PRIVATE);
Editor storeDataEditor = storeDataPref.edit();
storeDataEditor.clear();
storeDataEditor.putString("UR_NAME", elemValue).apply();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void readListToStore(上下文ctx、字符串文件名、列表listToStore)引发IOException{
SharedReferences storeDataPref=ctx.getSharedReferences(“UR\u KEY”,Context.MODE\u PRIVATE);
String elemValue=storeDataPref.getString(“UR_NAME”,null);
if(elemValue!=null){
类型listType=新类型令牌(){
}.getType();
listToStore=new Gson().fromJson(elemValue,listType);
}
}
公共静态void saveListToStore(上下文ctx、字符串文件名、列表listToStore)引发IOException{
字符串elemValue=“”;
Gson Gson=新的Gson();
试一试{
FileOutputStream FileOutputStream=ctx.openFileOutput(文件名,ctx.MODE\u PRIVATE);
elemValue=gson.toJson(listToStore);
SharedReferences storeDataPref=ctx.getSharedReferences(“UR\u KEY”,Context.MODE\u PRIVATE);
编辑器storeDataEditor=storeDataPref.edit();
storeDataEditor.clear();
putString(“UR_NAME”,elemValue.apply();
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
取自此问题:
“您需要向Gson提供有关您正在使用的列表的特定泛型类型(或与之一起使用的任何泛型类型)的信息。特别是在反序列化JSON时,它需要这些信息才能确定应将每个数组元素反序列化到哪种类型的对象
Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();
String s = gson.toJson(list, listOfTestObject);
List<TestObject> list2 = gson.fromJson(s, listOfTestObject);
Type listOfTestObject=new-TypeToken(){}.getType();
字符串s=gson.toJson(列表,listOfTestObject);
list2=gson.fromJson(s,listOfTestObject);
这一点记录在中。”
您可以将字符串写入和读取到文件中。列表可以是实现列表接口的任何集合类型尝试类似以下对象的方法A instanceOf Strings只是为了确保我们在同一页上,您发布了有效的方法和无效的方法您没有?请发布有问题的代码,以及输入、输出和预期输出的示例。+1因为这是一个有用的答案,也就是说,OP使用了一个定制的列表,而不是
java.util.list
上面的代码没有按预期工作,我想这是因为我自己的误解。我希望这个名单能让我通过我自己班级的名单。尝试了以下操作,但无法使用List newCardList=ArrayList();dataStore.saveListToStore(fn,newCardList);dataStore.readListToStore(fn,newCardList);