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

标签 java android firebase firebase-realtime-database

我遇到了错误

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 页面上


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

这是我的数据库

enter image description here

最佳答案

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

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

关于java - Firebase : Can't convert object of type java. lang.String 输入 com.example.g.Model.Cart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918583/

相关文章:

android - 检测在 Android 中工作的文件系统

firebase - 在没有唯一键的情况下向 firebase 发布请求

php - 通过 cURL/PHP 发送时,设备未收到 Firebase 通知

android - 关于将 Flutter 应用发布到 Play 商店的问题

javascript - 如何在注销时注销 FCM 设备 token 和服务 worker 以及如何重新初始化 Firebase 应用程序?

java - 在 Primefaces5+JSF2 中渲染不工作

java - 如何处理没有任何元素的浏览器通知弹出窗口?

java - 从内部存储中获取图像时出现 FileNotFoundException

java - Apache Camel ReSTLet 生产者连接器

android - SQLite 插入抛出语法异常