Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Retrieve object with ArrayList from Firebase

I am new to Firebase and facing the following issue:

I upload an object(Bill) to my firebase and one attribute is a ArrayList. And it works for uploading.

But when I retrieve the data I get the Error because of the ArrayList

com.firebase.client.FirebaseException: Failed to bounce to type

Firebase Structure

Main Activity:

Firebase billRef = new Firebase(getResources().getString(R.string.FB_billRef));

Query billQuery = billRef.orderByChild("status").equalTo("Ordered");
billQuery.addChildEventListener(
        new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Bill bill = dataSnapshot.getValue(Bill.class);

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        }
);

Bill.class:

public class Bill {

    private int billID;
    private int tableID;
    private int numCustomer;
    private String status;
    private ArrayList<Order> orders;


    public Bill(int billID, int tableID, int numCustomer,String status,ArrayList<Order> orders){
        this.billID = billID;
        this.tableID = tableID;
        this.numCustomer = numCustomer;
        this.status = status;
        this.orders = orders;
    }

    public ArrayList<Order> getOrders() {
        return orders;
    }

    public void setOrders(ArrayList<Order> orders){
        this.orders = orders;
    }
    public void addOrder(Order o){}

    public void delOrder(int orderID){}

    public int getBillID(){return billID;}
    public int getTableID(){return tableID;}
    public int getNumCustomer(){return numCustomer;}
    public String getStatus(){return status;}

}

How can I retrieve this type of firebase object to java object? Thanks a lot!

The only way I had to deal with this is break down the DataSnapshot attributes one by one and get the Order by using the datasnapshot.child("orders").getChildren()

But it is not a good approach to do it. Is it any method that I can get the Bill Object directly?

like image 312
Thomas Leung Avatar asked Mar 02 '26 01:03

Thomas Leung


1 Answers

I know I may be a little late for the one that asked, but at least for others... Using HashMaps instead of ArrayLists is helpful. Firebase is base on hashmaps, and therefore you will be able to access any attribute by key. In this case, instead of the Bills attribute, represented by an array list, I suggest you use a HashMap. (this is also helpful for updating keys)

like image 94
Honey Avatar answered Mar 03 '26 13:03

Honey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!