Reputation: 27
I am making an eCommerce app in which when Add to cart button is pressed in ProductDetails activity, the details of that respective product is added under the "Pending" collection with the product id as the document name with the fields as shown in the photo. Now there will be multiple users adding the products but they are filtered with the help of whereEqualsTo("uid", user.getUid()). So my question is when I place the order in placeOrder activity, I want to change the value of the state field form cart to order for that particular uid. How to do that???
placeOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db.collection("Pending")
.whereEqualTo("uid", user.getUid())
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Map<String, Object> cartMap = new HashMap<>();
cartMap.put("state", "orders");
db.collection("Pending").document().update(cartMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
finish();
}
});
}
});
}
});
Upvotes: 1
Views: 68
Reputation: 138824
There are two problems with your code. The first is the call of the .document()
method without passing any argument, which actually creates a new id. The second problem is that you are not looping to get the result. So to solve this, please use the following lines of code:
placeOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db.collection("Pending")
.whereEqualTo("uid", user.getUid())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
db.collection("Pending").document(document.getId()).update("state", "orders")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//finish();
}
});
}
}
}
});
}
});
Be also aware that calling finish();
will finish the activity when the first update completes, that's the reason why I commented that line.
Upvotes: 1