自定义对象的Android ArrayList - 保存到SharedPreferences - 可序列化?

Pau*_*ell 73 android arraylist serializable sharedpreferences

我有一个对象的ArrayList.该对象包含"Bitmap"和"String"类型,然后只包含两者的getter和setter.首先是Bitmap可序列化?

我如何将其序列化以将其存储在SharedPreferences中?我见过很多人问过类似的问题,但似乎没有人给出一个好的答案.如果可能的话,我更喜欢一些代码示例.

如果位图不可序列化,那么我该如何存储这个ArrayList?

非常感谢.

Moh*_*ran 132

是的,您可以将您的复合对象保存在共享首选项中.我们说..

 Student mStudentObject = new Student();
 SharedPreferences appSharedPrefs = PreferenceManager
             .getDefaultSharedPreferences(this.getApplicationContext());
 Editor prefsEditor = appSharedPrefs.edit();
 Gson gson = new Gson();
 String json = gson.toJson(mStudentObject);
 prefsEditor.putString("MyObject", json);
 prefsEditor.commit(); 
Run Code Online (Sandbox Code Playgroud)

..现在你可以检索你的对象:

 SharedPreferences appSharedPrefs = PreferenceManager
             .getDefaultSharedPreferences(this.getApplicationContext());
 Gson gson = new Gson();
 String json = appSharedPrefs.getString("MyObject", "");
 Student mStudentObject = gson.fromJson(json, Student.class);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请单击此处.

如果你想取回ArrayList任何类型的对象,例如Student,那么使用:

Type type = new TypeToken<List<Student>>(){}.getType();
List<Student> students = gson.fromJson(json, type);
Run Code Online (Sandbox Code Playgroud)

  • @Bhavik Metha,以上编写的代码不适用于Arraylists,上面的代码只是示例.如果你想找回任何对象ed学生的类型的Array List,那么我们:Type type = new TypeToken <List <Student >>(){}.getType(); List <Student> students = gson.fromJson(json,type); (11认同)

Spy*_*Zip 111

上面的答案有效,但不适用于列表:

为了保存对象列表,请执行以下操作:

List<Cars> cars= new ArrayList<Cars>();
    cars.add(a);
    cars.add(b);
    cars.add(c);
    cars.add(d);

    gson = new Gson();
    String jsonCars = gson.toJson(cars);
    Log.d("TAG","jsonCars = " + jsonCars);
Run Code Online (Sandbox Code Playgroud)

读取json对象:

Type type = new TypeToken<List<Cars>>(){}.getType();
List<Cars> carsList = gson.fromJson(jsonCars, type);
Run Code Online (Sandbox Code Playgroud)

  • import java.lang.reflect.Type; (9认同)
  • 这里的"Type"包是什么? (3认同)

Apu*_*kar 20

对我来说它的工作方式如下:

将值放在SharedPreferances中:

String key = "Key";
ArrayList<ModelClass> ModelArrayList=new ArrayList();

SharedPreferences shref;
SharedPreferences.Editor editor;
shref = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

Gson gson = new Gson();
String json = gson.toJson(ModelArrayList);

editor = shref.edit();
editor.remove(key).commit();
editor.putString(key, json);
editor.commit();
Run Code Online (Sandbox Code Playgroud)

要从SharedPreferances获取值:

Gson gson = new Gson();
String response=shref.getString(key , "");
ArrayList<ModelClass> lstArrayList = gson.fromJson(response, 
    new TypeToken<List<ModelClass>>(){}.getType());
Run Code Online (Sandbox Code Playgroud)


Met*_*ete 13

保存:

public static void saveSharedPreferencesLogList(Context context, List<PhoneCallLog> callLog) {
    SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = mPrefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(callLog);
    prefsEditor.putString("myJson", json);
    prefsEditor.commit();
}
Run Code Online (Sandbox Code Playgroud)

对于负载:

public static List<PhoneCallLog> loadSharedPreferencesLogList(Context context) {
    List<PhoneCallLog> callLog = new ArrayList<PhoneCallLog>();
    SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = mPrefs.getString("myJson", "");
    if (json.isEmpty()) {
        callLog = new ArrayList<PhoneCallLog>();
    } else {
        Type type = new TypeToken<List<PhoneCallLog>>() {
        }.getType();
        callLog = gson.fromJson(json, type);
    }
    return callLog;
}
Run Code Online (Sandbox Code Playgroud)

PhoneCallLog是我的自定义对象的名称.(包含String,long和boolean值)