Can't Convert Object Of Type Java.lang.string To Type
I have a problem after I add another table to my firebase database. it gives me following error:  com.google.firebase.database.DatabaseException: Can't convert object of type java.
Solution 1:
In your first code snippet you read from the root of the database. Since you're trying to read orders, you should read from /Orders instead:
databaseReference= FirebaseDatabase.getInstance().getReference();
firebaseHelper=newFirebaseHelper(databaseReference);
//myAdapter=new MyAdapter(this,firebaseHelper.retrieve());//rvOrder.setAdapter(myAdapter);
databaseReference.child("Orders").addValueEventListener(newValueEventListener() {
Now in your onDataChange you can read the orders by looping over them. Since you already do precisely that in FirebaseHelper.fetchData, you can call that method:
publicvoidonDataChange(DataSnapshot dataSnapshot) {
    firebaseHelper.fetchData(dataSnapshot);
}
Now all that is left is to wire up the data from firebaseHelper.orders to an adapter and the view:
publicvoidonDataChange(DataSnapshot dataSnapshot) {
    firebaseHelper.fetchData(dataSnapshot);
    myAdapter=newMyAdapter(this,firebaseHelper.orders);
    rvOrder.setAdapter(myAdapter);
}
This last step will require that you make FirebaseHelper.orders public, and probably some of the variables must be final.
Solution 2:
I think you need to update this method accordingly:
publicBooleansave(Order order) {
    if (order == null) {
        saved = false;
    } else {
        try {
            databaseReference.child("Orders").push().setValue(order);
            saved = true;
        } catch (DatabaseException e) {
            e.printStackTrace();
            saved = false;
        }
    }
    return saved;
}
Try to change:
databaseReference.child("Orders").push().setValue(order);
To:
databaseReference.child("Orders").child(order.getId).setValue(order);
Also, to retreive all Orders keep using the enhanced loop:
for(DataSnapshot ds : dataSnapshot.getChildren()) {
    System.out.println(ds.getValue(Order.class));
    Order order = ds.getValue(Order.class);
    orders.add(order);
}
Post a Comment for "Can't Convert Object Of Type Java.lang.string To Type"