简体   繁体   English

在Android中保存对象数组列表的方法

[英]Way of saving arraylist of objects in android

I am having difficulty saving an ArrayList of objects to a file in android. 我在将对象的ArrayList保存到android中的文件时遇到困难。 I want to be able be able to read these objects and call a method on each object to get one of their attributes. 我希望能够读取这些对象并在每个对象上调用方法以获取其属性之一。 Here is the object class 这是对象类

public class Transaction {

private double amount;
private String store;
private Date date;

public Transaction(){
    amount = 0.0;
    store = null;
    date = null;
}

public Transaction(double amt, String str, Date dat ) {
    amount = amt;
    store = str;
    date = dat;
}

} }

In my activity I take input and create a new Transaction object. 在我的活动中,我接受输入并创建一个新的Transaction对象。 I then add the Transaction object to an ArrayList. 然后,我将Transaction对象添加到ArrayList中。 I want to save the ArrayList to a file and be able to access the ArrayList from a different activity. 我想将ArrayList保存到文件中,并能够从其他活动访问ArrayList。 In this different activity I want to access and call getAmount() on each object in the ArrayList to create a total amount and display it on screen. 在这个不同的活动中,我想访问并调用ArrayList中的每个对象上的getAmount()以创建总量并将其显示在屏幕上。 Thanks. 谢谢。

You can implement Parcelable in your objects. 您可以在对象中实现Parcelable There is online tool that can helps. 在线工具可以提供帮助。 Ones you implement Parcelable you can pass the ArrayList<Parcelable> trough activities by putting the array in Intent's extras or save the array in SharedPreferences for later usage. 实现Parcelable的用户可以通过将ArrayList<Parcelable>槽活动传递到Intent的Extras中,或将其保存在SharedPreferences中供以后使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM