I upload my data class object to Firestore, it uploads fine, when I try to download it from another activity the java.lang.ClassCastException
occurs. I need the object of Cart class in AddToCart.java to be saved in the Firestore (Success) and get it back in Cart.java (Failure). PLEASE HELP!!!!!
Here is the how I am putting data into the 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)
Here, the map with one key value is uploaded to firestore:
NOW, here is the code to get this map back.
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.
}
}
}
}
});
Here is my CartModel.java (Data class)
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;
}
}
I see now in your database structure, that you hold more than one object of type CartModel
inside that document. To solve this, get the data as a Map
like in the following line of code:
Map<String, Object> map = document.getData();
Iterate through the map
and get all CartModel
objects from it. That's it.
I think issue in the Constant object. There is a mismatch between Constant.listCartItems and the object you put into the map.
You have two ways to solve your issue :
cast your Object to the correct one :
Object object = (MyObject) cartList.get(key);
^^^^^^^^
You have to set the Key and Value type :
Map<Key, Value> cartList = new HashMap<String,CartModel>();
^^^ ^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.