将 ArrayList<Custom Object> 保存到本地存储

Save ArrayList<Custom Object> to local storage

哦,天哪,所以我花了大约一个小时阅读并尝试在 Android 首选项和本地存储中存储 ArrayListParcelable Object 的不同方法,现在我回到原点。顺便说一句,我已经下载并导入了 Gson,但我无法让它工作。

基本上,我在一个名为 taskListArrayList<> 中有一个名为 Task 的 class,即使我的用户关闭了我的应用程序,我也需要保存它。 ObjectString、一些 Ints、一些 Booleans 和 class SubtaskArrayList 组成(如果重要的话)。重要的是,我似乎不能只把它写成一个字符串列表,我发现的所有教程都只显示保存像字符串这样的简单 ArrayList,它们都是可序列化的,而不是可打包的。

编辑:感谢您的建议,但我实施了 Parcelable 以将特定对象打包到活动之间的列表中。

您可以将自定义对象转换为 Json 格式作为一个字符串并存储在 SharedPreference 中。

已更新

例如,您的自定义对象是 Employee

public class Employee
{

    private String name = "test";
    private int age = 29;

    @Override
    public String toString()
    {
        return "Employee [age=" + age + ", name=" + name + "]";
    }
}

在 Activity 中,您可以将 Java 对象转换为字符串

Gson gson = new Gson();
Employee m_employee = new Employee();
String str = gson.toJson(m_employee);
System.out.println("===== " + str);

当用户关闭应用程序时,您可以将 str 存储在 SharedPreference 中。当用户打开应用程序时从 SharedPreference 获取。

根据Parcelable API

Parcelable: 类 的接口,其实例可以从 Parcel.

写入和恢复

并阅读Parcel API

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

所以如果你想通过一个 ArrayList 存储所有不同类型的引用,你必须将所有对象包装到一个公共接口,在这种情况下 Serializable 是你的朋友。

另外,根据你的问题:

I need to save even when my users close my app.

如果您检查 android 生命周期,您将看到您需要在 onStop() 中执行此操作,当您的 activity 不再对用户可见时调用此操作。

更多信息 来自 this question

首选项中的设置值:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

从首选项中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

更多信息:

Using Shared Preferences

Shared Preferences

你可以试试这个:

FileOutputStream fout = new FileOutputStream("c:\object.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourObject);

有关详细信息,请参阅此 link:- Here

我们可以保存自定义数组列表并使用 SharedPreference

获取它
  • 将数组列表值设置为 SharedPreference
public void SaveArrayList(ArrayList<CustomObj> listArray){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(listArray);
        editor.putString("TAG_LIST", json);  ///"TAG_LIST" is a key must same for getting data 
        editor.apply();     
    }

*用于从 SharedPrefrence 获取数组列表

   public ArrayList<CustomObj> getArrayList(){
      SharedPreferences prefs = 
        PreferenceManager.getDefaultSharedPreferences(getContext());
            Gson gson = new Gson();
            String json = prefs.getString("TAG_LIST", null);
            Type listType = new TypeToken<ArrayList<CustomObj>>() {}.getType();
            mSomeArraylist= gson.fromJson(json, listType);
        return gson.fromJson(json, listType);
}