Firebase : Can't convert object of type java.lang.String to type com.example.g.Model.Cart

£可爱£侵袭症+ 提交于 2020-01-30 11:43:39

问题


I had error that said

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.g.Model.Cart at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.5:423) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.5:214) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.5:79) at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.5:212) at com.example.gerobokgo.Customer.ViewCart$2.onDataChange(ViewCart.java:107)>

This error happen on my ViewCart page


if (currentUser != null) {
            userID = currentUser.getUid();
            cust_id = firebaseAuth.getUid();
            databaseReference = FirebaseDatabase.getInstance().getReference("Cart").child("Customer List").child(cust_id);
            recyclerView = findViewById(R.id.rv);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    CartList = new ArrayList<>();
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        for (DataSnapshot cart : ds.getChildren()) {
                            CartList.add(cart.getValue(Cart.class));

                        }
                    }

                    CartAdapter cartAdapter = new CartAdapter(CartList);
                    recyclerView.setAdapter(cartAdapter);
//                    total.setText( String.valueOf(totalPrice));

                }

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


public class Cart {

    public String cart_id;
    public String pro_id;
    public String cust_id;
    public String brand_id;
    public String pro_name;
    public String pro_price;
    public String pro_image;
    public String pro_category;
    public String quantity;
    public String size;
    public String date;


    public Cart () {
    }


    public Cart(String cart_id, String pro_id, String cust_id, String brand_id, String pro_name, String pro_price, String pro_image, String pro_category, String quantity, String size,String date) {
        this.cart_id = cart_id;
        this.pro_id = pro_id;
        this.cust_id = cust_id;
        this.brand_id = brand_id;
        this.pro_name = pro_name;
        this.pro_price = pro_price;
        this.pro_image = pro_image;
        this.pro_category = pro_category;
        this.quantity = quantity;
        this.size = size;
        this.date=date;
    }

this is my database


回答1:


You have two nested loops in your onDataChange:

CartList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
    for (DataSnapshot cart : ds.getChildren()) {
        CartList.add(cart.getValue(Cart.class));
    }
}

But if I look at the JSON at the location you attach the listener to, I see only the date level. So in that case, you need only one loop:

CartList = new ArrayList<>();
for (DataSnapshot cart : dataSnapshot.getChildren()) {
    CartList.add(cart.getValue(Cart.class));
}

The nested loop would only be needed if a user could have multiple carts per day, but your current data model only allows one cart per day.




回答2:


From your code it looks like you are iterating a layer too deep in your structure. Easy Fix: Replace

for (DataSnapshot ds : dataSnapshot.getChildren()) {
    for (DataSnapshot cart : ds.getChildren()) {
         CartList.add(cart.getValue(Cart.class));
    }
}

with

for (DataSnapshot cart : dataSnapshot.getChildren()) {
     CartList.add(cart.getValue(Cart.class));
}


来源:https://stackoverflow.com/questions/58918583/firebase-cant-convert-object-of-type-java-lang-string-to-type-com-example-g-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!