3

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?

9
  • did u try using List instead of ArrayList? Commented Jun 28, 2016 at 13:09
  • In your logcat, under that error message, there will be a stack trace. In that stack trace, it will specify the actual cause of the problem (typically after Caused by). Commented Jun 28, 2016 at 13:35
  • Try adding a no-argument constructor to Bill. Commented Jun 28, 2016 at 13:58
  • Re adolfosrs: I did, but the same error occurs. Commented Jun 28, 2016 at 14:43
  • @FrankvanPuffelen The error is caused by the ArrayList orders. Here's the errors: Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@1d5e2d2; line: 1, column: 32] (through reference chain: com.test.Class.Bill["orders"]) Commented Jun 28, 2016 at 14:45

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.