Firebase:无法将java.lang.String类型的对象转换为com.example.g.Model.Cart类型 [英] Firebase : Can't convert object of type java.lang.String to type com.example.g.Model.Cart

查看:63
本文介绍了Firebase:无法将java.lang.String类型的对象转换为com.example.g.Model.Cart类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说的错误

com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.g.Model.Cart类型 在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database @@ 16.0.5:423) com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database @@ 16.0.5:214) com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database @@ 16.0.5:79) com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database @@ 16.0.5:212) 在com.example.gerobokgo.Customer.ViewCart $ 2.onDataChange(ViewCart.java:107)>

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)>

此错误发生在我的ViewCart页面上

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;
    }

这是我的数据库

推荐答案

在您的onDataChange中有两个嵌套循环:

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));
    }
}

但是,如果我在将侦听器附加到的位置查看JSON,则只会看到日期级别.因此,在那种情况下,您只需要一个循环:

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.

这篇关于Firebase:无法将java.lang.String类型的对象转换为com.example.g.Model.Cart类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆