简体   繁体   中英

How to retrieve data from Firebase that have 2 layer structure?

I have 2 layer structure of table inside my Firebase. I have problem with retrieving this. How can I retrieve this data from my Firebase? I have provided my main code and my Order class.

在此处输入图像描述

here is my code

 databaseReference = FirebaseDatabase.getInstance().getReference("Order");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    orderList = new ArrayList<>();

                        for (DataSnapshot orderSnapshot : dataSnapshot.getChildren()) {

                            orderList.add(orderSnapshot.getValue(Order.class));
                        }

                    psOrderAdapter PsOrderAdapter = new psOrderAdapter(orderList);
                    recyclerView.setAdapter(PsOrderAdapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ManageOrder.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

public class Order {
    public String cust_id;
    public String pro_id;
    public String total;
    public String name;
    public String address;
    public String phone;
    public String status;
    public String date;
    public String dateTime;


    public Order() {
    }

    public Order(String cust_id, String pro_id, String total, String name, String address, String phone, String status,String date, String dateTime) {
        this.cust_id = cust_id;
        this.total = total;
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.pro_id = pro_id;
        this.status = status;
        this.date=date;
        this.dateTime=dateTime;
    }
//getter and setter
}

I'm expecting a result a result like this

在此处输入图像描述

Try the following:

for(DataSnapshot orderSnapshot : dataSnapshot.getChildren()) {
    for(DataSnapshot ds : orderSnapshot.getChildren()) {
         orderList.add(ds.getValue(Order.class));
    } 
}

Add another for loop to be able to retrieve the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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