自定义对象的 Android 数组列表-保存到共享首选项-可序列化?

我有一个对象的数组列表。对象包含类型“ Bitmap”和“ String”,然后只包含这两个类型的 getters 和 setter。首先,位图是可序列化的吗?

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

如果位图是不可序列化的,那么我如何存储这个数组列表?

94616 次浏览

是的,您可以在共享首选项中保存您的复合对象。

 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();

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

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

有关详细信息,请单击 给你。

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

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

上面的答案是可行的,但不适用于清单:

对于保存对象列表,如下所示:

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);

阅读 json 对象:

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

保存:

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();
}

负载:

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;
}

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

对我来说是这样的:

在 SharedPreferences 中输入值:

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();

要从 SharedPreferences 获取值:

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

上面的例子很有效。
下面是一个 科特林的例子

private var prefs: SharedPreferences = context?.getSharedPreferences("sharedPrefs", MODE_PRIVATE)
?: error("err")
private val gson = Gson()

拯救

fun saveObjectToArrayList(yourObject: YourObject) {
val bookmarks = fetchArrayList()
bookmarks.add(0, yourObject)
val prefsEditor = prefs.edit()


val json = gson.toJson(bookmarks)
prefsEditor.putString("your_key", json)
prefsEditor.apply()
}

fun fetchArrayList(): ArrayList<YourObject> {
val yourArrayList: ArrayList<YourObject>
val json = prefs.getString("your_key", "")


yourArrayList = when {
json.isNullOrEmpty() -> ArrayList()
else -> gson.fromJson(json, object : TypeToken<List<Feed>>() {}.type)
}


return yourArrayList
}

Gson实现 kotlin 扩展了@SpyZips 的答案,

序列化到 JSON

val jsonCars: String = Gson().toJson(cars);

反序列化以返回到对象列表

val type = object: TypeToken<List<Car>>(){}.type
val carsList: List<Car> = Gson().fromJson(jsonCars, type)

解决方案

不要担心,你可以按照我的代码。我使用自己的自定义类共享首选项保存任何类型的列表在共享首选项。

public class AuthPreference {
private static final String MY_PREFERENCES = "MY_PREFERENCES";
private static final int MODE = Context.MODE_PRIVATE;
private static AuthPreference pref;
private final SharedPreferences sharedPreference;
private final SharedPreferences.Editor editor;


@SuppressLint("CommitPrefEdits")
public AuthPreference(Context context) {
sharedPreference = context.getSharedPreferences(MY_PREFERENCES, MODE);
editor = sharedPreference.edit();
}


public void clear() {
editor.clear().commit();


}


//Here  save your api responce like YourModel arraylist


public void saveDataResponce(ArrayList<YourModel> model_, String key) {
SharedPreferences.Editor editor = sharedPreference.edit();
Gson gson = new Gson();
String json = gson.toJson(model_);
editor.putString(key, json);
editor.apply();
}


//Here we get our api responce like YourModel arraylist.Using key value when you want to define.


public ArrayList<YourModel> getDataResponce(String key) {
Gson gson = new Gson();
String json = sharedPreference.getString(key, null);
Type type_ = new TypeToken<ArrayList<YourModel>>() {
}.getType();
return gson.fromJson(json, type_);
}
}

你可使用以下连结

public static String MyClassListToJsonString(List<MyClassEntry> myMyClassList){
Gson myGson = new Gson();
return myGson.toJson(myMyClassList);
}


public static List<MyClass> JsonStringToDepositList(String jsonString){
Gson myGson = new Gson();
Type type = new TypeToken<List<MyClassEntry>>(){}.getType();
return myGson.fromJson(jsonString, type);
}