【问题标题】:Firebase : Can't convert object of type java.lang.String to type com.example.g.Model.CartFirebase:无法将 java.lang.String 类型的对象转换为 com.example.g.Model.Cart 类型
【发布时间】:2019-11-19 00:16:50
【问题描述】:

我有错误说

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

这个错误发生在我的 ViewCart 页面上


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

这是我的数据库

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    onDataChange 中有两个嵌套循环:

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

    但是,如果我在您附加侦听器的位置查看 JSON,我只会看到日期级别。所以在这种情况下,你只需要一个循环:

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

    只有当用户每天可以有多个购物车时才需要嵌套循环,但您当前的数据模型每天只允许一个购物车。

    【讨论】:

    • 领先我两分钟!
    【解决方案2】:

    从您的代码看来,您在结构中迭代了一个太深的层。轻松修复:替换

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2021-07-16
      • 2018-07-08
      • 1970-01-01
      • 2018-05-22
      • 2020-12-24
      相关资源
      最近更新 更多