【问题标题】:Storing an arraylist of custom objects (which have Strings, Dates, Lists, and more as fields) in shared preferences在共享首选项中存储自定义对象的数组列表(具有字符串、日期、列表等字段)
【发布时间】:2016-08-24 14:03:58
【问题描述】:

我正在尝试将客户端的数组列表保存在共享首选项中,但是出现内存不足错误。我是新手,不知道该怎么做?我在 stackoverflow 上查看了很多页面,但找不到适合我的页面,或者也没有自定义对象的 ArrayList,其中每个对象都包含更多带有自定义对象的 ArrayList。

客户对象:

public class Client implements Serializable, Comparable<Client> {
    private int clientID;
    private String name;
    private String phone;
    private String email;
    private String url;
    private Double turnover;
    private String visitAddress;
    private String visitCity;
    private String visitZipcode;
    private String visitCountry;
    private String postalAddress;
    private String postalCity;
    private String postalZipcode;
    private String postalCountry;
    private Drawable clientImage;
    private List<Contact> contactList =  new ArrayList<Contact>();
    private List<Project> projectList = new ArrayList<Project>();
    private List<Task> taskList = new ArrayList<Task>();
    private List<Order> orderList = new ArrayList<Order>();


    public Client(int clientID, String Name, String Phone, String Email, String URL, Double Turnover,
                  String VisitAddress, String VisitCity, String VisitZipcode, String VisitCountry,
                  String PostalAddress, String PostalCity, String PostalZipcode, String PostalCountry,
                  List contactList, List projectList, List taskList, List orderList){
            super();
        this.clientID = clientID;
        this.name = Name;
            this.phone = Phone;
            this.email = Email;
            this.url = URL;
            this.turnover = Turnover;
            this.visitAddress = VisitAddress;
            this.visitCity = VisitCity;
            this.visitZipcode = VisitZipcode;
            this.visitCountry = VisitCountry;
            this.postalAddress = PostalAddress;
            this.postalCity = PostalCity;
            this.postalZipcode = PostalZipcode;
            this.postalCountry = PostalCountry;
            this.contactList = contactList;
            this.projectList = projectList;
            this.taskList = taskList;
            this.orderList = orderList;
    }


    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getEmail() {
        return email;
    }

    public String getUrl() {
        return url;
    }

    public Double getTurnover() {

        return turnover;
    }

    public String getVisitAddress() {
        return visitAddress;
    }

    public String getVisitCity() {
        return visitCity;
    }

    public String getVisitZipcode() {
        return visitZipcode;
    }

    public String getVisitCountry() {
        return visitCountry;
    }

    public String getPostalAddress() {
        return postalAddress;
    }

    public String getPostalCity() {
        return postalCity;
    }

    public String getPostalZipcode() {
        return postalZipcode;
    }

    public String getPostalCountry() {
        return postalCountry;
    }

    public List<Contact> getContactList(){
        return contactList;
    }

    public List<Project> getProjectList(){
        return projectList;
    }

    public List<Task> getTaskList(){
        return taskList;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setTurnover(Double turnover) {
        this.turnover = turnover;
    }

    public void setVisitAddress(String visitAddress) {
        this.visitAddress = visitAddress;
    }

    public void setVisitCity(String visitCity) {
        this.visitCity = visitCity;
    }

    public void setVisitZipcode(String visitZipcode) {
        this.visitZipcode = visitZipcode;
    }

    public void setVisitCountry(String visitCountry) {
        this.visitCountry = visitCountry;
    }

    public void setPostalAddress(String postalAddress) {
        this.postalAddress = postalAddress;
    }

    public void setPostalCity(String postalCity) {
        this.postalCity = postalCity;
    }

    public void setPostalZipcode(String postalZipcode) {
        this.postalZipcode = postalZipcode;
    }

    public void setPostalCountry(String postalCountry) {
        this.postalCountry = postalCountry;
    }

    public Drawable getClientImage() {
        return clientImage;
    }

    public void setClientImage(Drawable clientImage) {
        this.clientImage = clientImage;
    }

    public int getClientID() {
        return clientID;
    }

    public void setClientID(int clientID) {
        this.clientID = clientID;
    }

基础项目对象:(也带有自定义对象列表)

public class Project implements Serializable, Comparable<Project>{

    private String clientName;
    private String projectName;
    private String projectDiscription;
    private String  projectStatus;
    private GregorianCalendar projectDate;
    private List<TimeSheet> projectTimeRegestrationList = new ArrayList<>();
    private List<WorkOrder> workOrderList = new ArrayList<WorkOrder>();


    public Project(String clientName, String projectName, String projectDiscription, String projectStatus, GregorianCalendar projectDate, List projectTimeRegestrationList, List workOrderList) {
        this.projectName = projectName;
        this.projectDiscription = projectDiscription;
        this.projectStatus = projectStatus;
        this.projectDate = projectDate;
        this.clientName = clientName;
        this.projectTimeRegestrationList = projectTimeRegestrationList;
        this.workOrderList = workOrderList;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectDiscription() {
        return projectDiscription;
    }

    public void setProjectDiscription(String projectDiscription) {
        this.projectDiscription = projectDiscription;
    }

    public String getProjectStatus() {
        return projectStatus;
    }

    public void setProjectStatus(String projectStatus) {
        this.projectStatus = projectStatus;
    }

    public GregorianCalendar getProjectDate() {
        return projectDate;
    }

    public void setProjectDate(GregorianCalendar projectDate) {
        this.projectDate = projectDate;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public List<TimeSheet> getProjectTimeRegestrationList() {
        return projectTimeRegestrationList;
    }

    public List<WorkOrder> getWorkOrderList() {
        return workOrderList;
    }

现在我的具体问题是:是否可以将其保存在共享首选项中,如果是,我该怎么做,如果不是,我应该如何在本地保护这些数据。

【问题讨论】:

  • 使用 Gson 库将您的对象转换为 json 字符串,反之亦然。并将字符串保存在首选项中。
  • @USKMobility,这对我不起作用,我得到了 OutOfMemoryError,但这甚至适用于自定义对象,包含自定义对象的列表,也包含自定义对象的列表
  • 您可以随时将数据保存到项目中某处的文件中或计算机中的特定路径中。
  • 你应该在你的自定义对象上实现 Serializable 并使用 SharedPreferences 的 write Serializable
  • @D.Blazer 检查它对我有用的答案

标签: java android arraylist save sharedpreferences


【解决方案1】:

您可以使用 json 将它们作为字符串存储在 sharedpreference 中

例如,如果您想存储包含 url、国家和电话的客户端类的对象

只写下面的

try {
                JSONArray jsonArray=new JSONArray();
                JSONObject object1=new JSONObject();
                object1.put("url","myurl");
                object1.put("phone","myphonenumber");
                object1.put("country","mycountry");
                jsonArray.put(object1);

                //adding another object
                JSONObject object2=new JSONObject();
                object2.put("url","anotherurl");
                object2.put("phone","anotherphonenumber");
                object2.put("country","anothercountry");
                jsonArray.put(object2);


            } catch (JSONException e) {
                e.printStackTrace();
            }

而不仅仅是通过在 sharedpreference 中调用 jsonArray.toString() 将其保存为字符串

如果您要存储的对象包含另一个对象的列表,您可以轻松保存,例如,如果您有要保存的 url、国家和电话号码以及包含第一个对象的另一个对象的列表和姓氏,您可以将列表保存为 json 对象本身,就像您为 url、电话号码和国家所做的那样,如下所示

try {
                JSONArray jsonArray=new JSONArray();
                JSONObject object1=new JSONObject();
                object1.put("url","myurl");
                object1.put("phone","myphonenumber");
                object1.put("country","mycountry");

                //saving a list of another object contaning firstname and lastname
                JSONArray listArray=new JSONArray();

                //supposing your list contains 10 objects
                for(int f=0;f<10;f++){
                    JSONObject listobject=new JSONObject();
                    listobject.put("firstname","myfirstname");
                    listobject.put("lastname","mylastname");
                    listArray.put(listobject);
                }
                //now the list array we added as an object of the jsonArray variable
                JSONObject object2=new JSONObject(); //object2 containing the list of firstname and lastname
                object2.put("list",listArray.toString());
                jsonArray.put(object1);//object1 contains the url, phone, and country
                jsonArray.put(object2);//object2 contains the list of firstname and lastname


            } catch (JSONException e) {
                e.printStackTrace();
            }

【讨论】:

  • 这是一个很好的方法,只要层次结构不那么深,就可以很好地工作,最终在一个巨大的结构变化之后使用它,因为我的层次结构深入了大约 5 个自定义对象的数组列表。这最多只能与 2 一起使用。但这是唯一的方法吗,我是不是刚开始构建我的应用程序真的很愚蠢,还是有一种好方法可以保存具有非常深层次结构的自定义对象的数组列表。
  • @D.Blazer 不,它可以随心所欲地工作,您可以添加一个函数,该函数为每个类返回一个 jsonobject,另一个函数返回一个 jsonarray 的对象数组列表这个类,您只需将其作为另一个 jsonobject 返回。我已经完成了保存一个包含至少 4 个不同对象的数组列表的类,但它需要比使用 Serializable 更多的工作,但一点也不难
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 2019-12-17
  • 2021-12-30
  • 2015-09-03
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多