'Loop through a string arrayList while using a string on firebase
So I have a simple ecommerce app that has products(custom objects) on the cart and a proceed to checkout button. When the button is pressed I send an ArrayList with product ids with the intent so I can access them when confirming the order. Now I need to loop through this list and make a separate order for each item on firebase(because there a are multiple venders involved). But when I do this it keeps duplicating the list of items on my home screen when it opens it as though its an infinite loop that keeps calling the home screen intent. So here is the ConfirmOrderActivity confirmOrder code
private void confirmOrder() {
loadingBar.show();
String locationJson = null;
if (useCurrentLocation.isChecked()){
Gson gson = new Gson();
locationJson = gson.toJson(location);
}
final String saveCurrentDate, saveCurrentTime;
Calendar callForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(callForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(callForDate.getTime());
final DatabaseReference ordersRef = FirebaseDatabase.getInstance().getReference()
.child("Orders")
.child(Prevalent.currentOnlineUsr.getPhone());
DatabaseReference productsRef = FirebaseDatabase.getInstance().getReference().child("Products");
for (String ID : products) {
String orderID = UUID.randomUUID().toString();
String finalLocationJson = locationJson;
productsRef.child(ID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
ProductModel model = snapshot.getValue(ProductModel.class);
AdminOrder order = new AdminOrder();
order.setOrderID(orderID);
order.setTotalAmount(model.getPrice());
order.setName(deliveryName.getText().toString());
order.setPhone(deliveryPhone.getText().toString());
order.setUid(Prevalent.currentOnlineUsr.getPhone());
order.setCity(deliveryCity.getText().toString());
order.setAddress(deliveryAddress.getText().toString());
if (useCurrentLocation.isChecked()) {
order.setLocation(finalLocationJson);
}
order.setDate(saveCurrentDate);
order.setTime(saveCurrentTime);
order.setState("Not delivered");
order.setMerchantID(model.getMerchantID());
order.setProduct(model.getPname());
order.setProductID(model.getPid());
ordersRef.child(ID).child(orderID).setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
FirebaseDatabase.getInstance().getReference()
.child("Cart")
.child("user")
.child(Prevalent.currentOnlineUsr.getPhone())
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
removeProductsFromDB(products);
loadingBar.dismiss();
showToast("Order Placed");
Intent intent = new Intent(ConfirmOrderActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
showToast("Failed");
loadingBar.dismiss();
}
});
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
showToast("Failed");
loadingBar.dismiss();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
I am new to programming so I am sure there should be a better way of doing this. Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
