SharedReference在一个单独的类中?

cld4siwp  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(355)

我正在尝试创建一个类,该类将包含用于保存对象的arraylist并将其加载到SharedReference中的方法。但是,当我尝试加载以前保存的数据时。就好像它没有拯救。我正在使用谷歌的gson库来帮助你。
示例\读\写.java

  1. public class Instance_Read_Write {
  2. public void saveData(Context context, ArrayList<Example> Examples){
  3. SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
  4. SharedPreferences.Editor editor = sharedPreferences.edit();
  5. Gson gson = new Gson();
  6. String json = gson.toJson(Examples);
  7. editor.putString("list", json);
  8. editor.apply();
  9. }
  10. public void loadData(Context context, ArrayList<Example> Examples) {
  11. SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
  12. Gson gson = new Gson();
  13. String json = sharedPreferences.getString("list", null);
  14. Type type = new TypeToken<ArrayList<Example>>() {}.getType();
  15. Examples = gson.fromJson(json, type);
  16. if (Examples == null) {
  17. Examples = new ArrayList<>();
  18. }
  19. }
  20. }

这就是我引用它们的方式。
主活动.java

  1. Instance_Read_Write instance_read_write = new Instance_Read_Write();
  2. instance_read_write.loadData(context,Examples);
  3. Examples.add(new Example("test"));
  4. ...
  5. ...
  6. ...
  7. instance_read_write.saveData(context,Examples);

有没有办法让这一切顺利?或者更好的方法?

ffdz8vbo

ffdz8vbo1#

这解决了您发布的代码中的一个错误,即 Examples 方法中的参数 loadData 方法。
既然你的意图 loadData 总是生成一个arraylist,然后我建议返回示例。或者,您必须首先创建一个空的arraylist并传入它,然后对其进行操作。
所以使用返回arraylist的方法:

  1. public ArrayList<Example> loadData(Context context) {
  2. ArrayList<Example> result;
  3. SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
  4. Gson gson = new Gson();
  5. String json = sharedPreferences.getString("list", null);
  6. Type type = new TypeToken<ArrayList<Example>>() {}.getType();
  7. result = gson.fromJson(json, type);
  8. if (result == null) {
  9. result = new ArrayList<>();
  10. }
  11. return result;
  12. }
  13. // ... and the calling code (in MainActivity) looks like:
  14. Examples = instance_read_write.loadData(context);

不要让“传递值”和“传递引用”的讨论火上浇油——这似乎是您所期待的 Example 要通过在方法体中指定来更新的实际参数-它不会-您只能对对象进行操作以影响它:https://stackoverflow.com/a/40523/2711811.
为了完成这个主题,这里有一个解决方案,它将继续您传递 Examples 对象:

  1. // In calling method...
  2. Examples = new ArrayList<Example>;
  3. instance_read_write.loadData(context,Examples);
  4. //...in class Instance_Read_Write...
  5. public void loadData(Context context, ArrayList<Example> Examples) {
  6. SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
  7. Gson gson = new Gson();
  8. String json = sharedPreferences.getString("list", null);
  9. Type type = new TypeToken<ArrayList<Example>>() {}.getType();
  10. ArrayList<Example> t = gson.fromJson(json, type);
  11. if (t != null) {
  12. Examples.addAll(t);
  13. }
  14. }
展开查看全部
p4tfgftt

p4tfgftt2#

  1. Examples = gson.fromJson(json, Example.class);

告诉我是否有效

相关问题