1

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: Firestore image

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

}

Debug image

11
  • What type of object gives cartList.get(key)? Commented Feb 18, 2019 at 15:48
  • Please add the content of your CartModel class and please responde with @AlexMamo Commented Feb 18, 2019 at 16:16
  • added the data class Commented Feb 18, 2019 at 17:05
  • @AlexMamo kindly see this. Commented Feb 18, 2019 at 17:20
  • @AlexMamo Kindly see this Commented Feb 18, 2019 at 17:21

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You have two ways to solve your issue :

Solution one

cast your Object to the correct one :

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

Solution two

You have to set the Key and Value type :

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

3 Comments

1> It is redundant cast
2> I don't control the cartList type, its coming from the server
Map<String,Object> are the parameters from which the values are coming to the server.
1

I think issue in the Constant object. There is a mismatch between Constant.listCartItems and the object you put into the map.

1 Comment

Error occur when I try to cast object to CartModel class.
1

For Firestore just do document.toObject(Pojo.class)

wolah !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.