Android保存自定义对象数组

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

问题描述

我有一个关于保存自定义对象数组列表的问题.我有一个名为 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 对象并将它们添加到我的名为 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 保存在设备的存储空间中,以便随时访问.我曾经将数据保存为字符串或一些带有共享首选项的整数,但我不能用它保存这个 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,并将数据存储在文件中.如果您想实现序列化,请参阅此解决方案.

使用 SQLite 存储数据.查看本教程 开始.

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

您可能想阅读 this 也是!

EDIT : You might want to read this as well!

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)