簡體   English   中英

在Android中保存對象數組列表的方法

[英]Way of saving arraylist of objects in android

我在將對象的ArrayList保存到android中的文件時遇到困難。 我希望能夠讀取這些對象並在每個對象上調用方法以獲取其屬性之一。 這是對象類

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;
}

}

在我的活動中,我接受輸入並創建一個新的Transaction對象。 然后,我將Transaction對象添加到ArrayList中。 我想將ArrayList保存到文件中,並能夠從其他活動訪問ArrayList。 在這個不同的活動中,我想訪問並調用ArrayList中的每個對象上的getAmount()以創建總量並將其顯示在屏幕上。 謝謝。

您可以在對象中實現Parcelable 在線工具可以提供幫助。 實現Parcelable的用戶可以通過將ArrayList<Parcelable>槽活動傳遞到Intent的Extras中,或將其保存在SharedPreferences中供以后使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM