【发布时间】:2019-07-12 00:59:27
【问题描述】:
我将我的数据类对象上传到 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:
现在,这里是获取此地图的代码。
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;
}
}
【问题讨论】:
-
什么类型的对象给
cartList.get(key)? -
请添加您的
CartModel课程内容并回复@AlexMamo -
添加数据类
-
@AlexMamo 请看这个。
-
@AlexMamo 请看这个
标签: java android firebase hashmap google-cloud-firestore