java.lang.ClassCastException:java.util.HashMap无法转换为自定义数据类

时间:2019-02-18 15:40:42

标签: java android firebase hashmap google-cloud-firestore

我将数据类对象上传到Firestore,它上传很好,当我尝试从其他活动中下载它时,发生java.lang.ClassCastException。 我需要将AddToCart.java中的Cart类的对象保存在Firestore中(成功),并在Cart.java中将其取回(失败)。请帮助!!!!!

这是我将数据放入Firestore的方式:

 private void updateFirestore() {

    Map cartList = new HashMap<String,CartModel>();

    //I put data into the map

    FirebaseFirestore firestoreRef = FirebaseFirestore.getInstance();
    firestoreRef.collection(colloctionKeyTitle)
                  .document(currentSelectedModel.getUserId()).set(cartList)

在这里,将具有一个键值的地图上传到firestore: Firestore image

现在,这是获取此地图的代码。

 FirebaseFirestore firestoreRef = FirebaseFirestore.getInstance();

    Task<DocumentSnapshot> documentSnapshot = firestoreRef.collection(COLLECTION_ID).document(userId).get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    cartList = task.getResult().getData();
                    if (cartList != null) {
                        if (!cartList.isEmpty()) {
                            for (String key : cartList.keySet()) {
                                Object object = cartList.get(key);// In object variable I see my data when debug, but app crashes on next line.
                                Constant.listCartItems.add((CartModel) object); //this is the line where error occur.
                            }
                        }

                    }
                }
            });

这是我的CartModel.java(数据类)

public class CartModel {
private String productTitle,productPrice, featuredImagePath, quantity;

public CartModel(String productTitle, String productPrice, String featuredImagePath,String quantity) {
    this.productTitle = productTitle;
    this.productPrice = productPrice;
    this.quantity=quantity;
    this.featuredImagePath = featuredImagePath;
}

public CartModel(String productTitle, String productPrice, String featuredImagePath) {
    this.productTitle = productTitle;
    this.productPrice = productPrice;
    this.featuredImagePath = featuredImagePath;
}

public CartModel() {

}


public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getProductTitle() {
    return productTitle;
}

public void setProductTitle(String productTitle) {
    this.productTitle = productTitle;
}

public String getProductPrice() {
    return productPrice;
}

public void setProductPrice(String productPrice) {
    this.productPrice = productPrice;
}

public String getFeaturedImagePath() {
    return featuredImagePath;
}

public void setFeaturedImagePath(String featuredImagePath) {
    this.featuredImagePath = featuredImagePath;
}

}

Debug image

3 个答案:

答案 0 :(得分:1)

我现在在您的数据库结构中看到,您在该文档中拥有多个CartModel类型的对象。要解决此问题,请像下面的代码行一样以Map的形式获取数据:

Map<String, Object> map = document.getData();

遍历map并从中获取所有CartModel个对象。就是这样。

答案 1 :(得分:0)

我认为Constant对象存在问题。 Constant.listCartItems与您放入地图中的对象之间不匹配。

答案 2 :(得分:0)

您有两种解决问题的方法:

解决方案一

将对象投射到正确的对象上:

Object object = (MyObject) cartList.get(key);
                 ^^^^^^^^

解决方案二

您必须设置键和值类型:

Map<Key, Value> cartList = new HashMap<String,CartModel>(); 
    ^^^   ^^^^