如何在Arraylist数据中检索Firebase json ArrayList并将其存储在Arraylist中以在以后的Textview中显示

时间:2019-04-29 15:53:36

标签: android firebase firebase-realtime-database

enter image description here
我想从火灾库中检索数据,以便稍后在“文本视图”中显示。但数据快照并没有使所有键和值(即date,month,year)仅达到date。我需要在单个文本视图中约会月份和年份。 为了计时,我在Fire-base中将数据作为String插入。

我确实尝试将日期值添加到Array-list中,但显示为空。当我检查调试器时。我对调试了解得很少。

问题在第二个FOR循环中

我还需要显示帐户和金额。在列表视图中的日期(完成日期),我需要显示的帐户和金额

ArrayList<String> account;
ArrayList<String> amount;
ArrayList<ArrayList<String>> datedata;

final DateDataHelper dateDataHelper = new DateDataHelper(AccountBillList.this, date, month, year);

myRef = database.getReference("purchasebill");
myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                billlistHelper = ds.getValue(BilllistHelper.class);
                assert billlistHelper != null;
                account.add(billlistHelper.getAccount());
                amount.add(billlistHelper.getAmount());
                for (DataSnapshot dd : ds.child("datedata").getChildren(){
                    dateDataHelper = dd.getValue(DateDataHelper.class);

                    date.add(dateDataHelper.getDate());
                    month.add(dateDataHelper.getMonth());
                    year.add(dateDataHelper.getYear());
                }
                intendby.add(billlistHelper.getIntendby());
                vendorname.add(billlistHelper.getVendorName());

            }
            billListAdapter.notifyDataSetChanged();
        }


class DateDataHelper {
    private String date;
    private String month;
    private String year;

public DateDataHelper(AccountBillList accountBillList, ArrayList<String> date, ArrayList<String> month, ArrayList<String> year) {
}

public DateDataHelper(String date, String month, String year) {
    this.date = date;
    this.month = month;
    this.year = year;
}
// getter and setter are there over here
}

1 个答案:

答案 0 :(得分:0)

您要引用节点purchasebill,然后在purchasebill的直接子项gameworlddatamination中循环。

要解决您的问题,您可以添加对子项datamination的引用,这样它就不会在gameworld内循环,因此您可以执行以下操作:

myRef.child("datamination").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            billlistHelper = dataSnapshot.getValue(BilllistHelper.class);
            assert billlistHelper != null;
            account.add(billlistHelper.getAccount());
            amount.add(billlistHelper.getAmount());
            for (DataSnapshot dd : dataSnapshot.child("datedata").getChildren(){
                dateDataHelper = dd.getValue(DateDataHelper.class);

                date.add(dateDataHelper.getDate());
                month.add(dateDataHelper.getMonth());
                year.add(dateDataHelper.getYear());
            }
            intendby.add(billlistHelper.getIntendby());
            vendorname.add(billlistHelper.getVendorName());

        }
        billListAdapter.notifyDataSetChanged();

}