Android的保存自定义对象的数组 [英] Android save array of custom objects

查看:1974
本文介绍了Android的保存自定义对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于保存自定义对象的ArrayList的问题。我有一类称为notitie:

I have a question about saving an arraylist of custom objects. I have a class called notitie:

public class Notitie implements Serializable{

private String titel = "";
private String type = "";
private String datum = "";

public void setTitel (String titel){
    this.titel = titel;
}
public String getTitel(){
    return titel;
}
public void setType (String type){
    this.type = type;
}
public String getType(){
    return type;
}
public void setDatum (String datum){
    this.datum = datum;
}
public String getDatum(){
    return datum;
}
}

创建Notitie的一些对象,并把它们添加到我的ArrayList称为notities

I create some objects of Notitie and add them to my arraylist called notities

ArrayList<Notitie> notities = new ArrayList<Notitie>();

Notitie notitie1 = new Notitie();
notitie1.setTitel("Meting");
notitie1.setType("Watermeting");
notitie1.setDatum("22-09-12");
notities.add(notitie1);

Notitie notitie2 = new Notitie();
notitie1.setTitel("Meting2");
notitie1.setType("Watermeting2");
notitie1.setDatum("23-09-12");
notities.add(notitie2);

Notitie notitie3 = new Notitie();
notitie1.setTitel("Meting3");
notitie1.setType("Watermeting3");
notitie1.setDatum("24-09-12");
notities.add(notitie3);

现在我要保存填充的ArrayList在设备的存储中,这样就可以随时访问。我用来保存数据作为一个字符串或某些整数与共享preferences,但我无法保存此ArrayList这一点。 是否有人有办法解决吗?

Now I want to save the filled Arraylist on the device's storage so it can be accessed anytime. I used to save data as a String or some Integers with sharedpreferences but I can't save this Arraylist with that. Does anybody have a solution?

在此先感谢!

推荐答案

您做有几个选项:

  1. 使用序列化,XML或JSON和存储数据的文件。请参照<一个href="http://stackoverflow.com/questions/4393889/how-can-i-store-an-arraylist-of-custom-objects">this如果你想实现序列化的解决方案的。

存储你的数据使用SQLite。看一看本教程得到启动。

Store you data using SQLite. Have a look at this tutorial to get started.

编辑::您可能需要阅读<一个href="http://stackoverflow.com/questions/3580351/what-is-the-best-way-to-save-the-contents-of-an-arraylist">this还有!

EDIT : You might want to read this as well!

这篇关于Android的保存自定义对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆