0

This is how my cart looks likeI am a beginner I am trying to recreate the commerce apps and the following picture show items on a cart waiting to be ordered. I am using Cloud Firestore. I want users to click on place order button and all items get added to another database and the cart get cleared. How can I go about it?

This is the code in CartActivity

public class CartFragment extends Fragment {

 private RecyclerView MyRecycler;
 private Context context;
 private CartAdapter adapter;
 Button buttonorder;
 private FirebaseFirestore db = FirebaseFirestore.getInstance();
 private FirebaseAuth  mAuth = FirebaseAuth.getInstance();
 private FirebaseUser  user = mAuth.getCurrentUser();



@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
 ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.cartfragment_layout, container, 
   false);
    MyRecycler = v.findViewById(R.id.recycler_cart);
    buttonorder = v.findViewById(R.id.place_order_btn);
    setUpRecyclerView();

    buttonorder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // placeOrder();
        }
    });
    return v;
 }


 private void setUpRecyclerView() {
    CollectionReference notebookRef;
    mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    if (user != null) {
        notebookRef = db.collection(String.valueOf(new 
  StringBuffer(user.getUid()).append("cart")));
        Query query = notebookRef.orderBy("delivery", 
   Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<CartModel> options = new 
   FirestoreRecyclerOptions.Builder<CartModel>()
                .setQuery(query, CartModel.class)
                .build();

        adapter = new CartAdapter(options, getActivity(), user);
        adapter.notifyDataSetChanged();
        MyRecycler.setHasFixedSize(true);
        MyRecycler.setLayoutManager(new LinearLayoutManager(context));
        MyRecycler.setAdapter(adapter);


        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, 
            RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder 
            target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int 
             direction) {
                adapter.deleteItem(viewHolder.getAdapterPosition());
            }
           }).attachToRecyclerView(MyRecycler);
          }
         }

 @Override
 public void onStart() {
    super.onStart();
    adapter.startListening();
 }

 @Override
 public void onStop() {
    super.onStop();
    adapter.stopListening();
  }


}

And this is my Adapter

public class CartAdapter extends FirestoreRecyclerAdapter<CartModel, 
CartAdapter.CartViewHolder> {
   Context context;
   FirebaseUser user;

public CartAdapter(@NonNull FirestoreRecyclerOptions<CartModel> options, 
    Context context, FirebaseUser user) {
    super(options);
    this.context = context;
    this.user = user;

}

@Override
protected void onBindViewHolder(@NonNull CartViewHolder holder, int 
   position, @NonNull CartModel model) {

    holder.name.setText(model.getItemName());
    holder.delivery.setText(model.getDelivery());
    holder.price.setText(model.getamount());
}


public void deleteItem(int position) {
    getSnapshots().getSnapshot(position).getReference().delete();

}


@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int 
                      i) {
    View view;
    LayoutInflater mInflater = 
    LayoutInflater.from(viewGroup.getContext());
    view = mInflater.inflate(R.layout.activity_cart_adapter, viewGroup, 
        false);
    return new CartViewHolder(view);
}

public class CartViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public TextView delivery;
    public TextView price;


    public CartViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.product_name3);
        delivery = itemView.findViewById(R.id.product_delivery3);
        price = itemView.findViewById(R.id.price3);


    }
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1) Loop over the items in the cart, 2) Add each item to the new location, 3) Delete the item from the old location. – Frank van Puffelen May 25 '19 at 14:26
  • @FrankvanPuffelen I tried this https://stackoverflow.com/questions/52154502/iterate-through-sub-collections-to-get-fields-firestore-android but still not working ` The error is com.google.firebase.firestore.QuerySnapshot cannot be cast to java.util.List –  May 25 '19 at 17:01
  • 2
    Which is a much smaller problem that what you've posted. If you reproduce this problem in a minimal amount of code (for example: you won't need any UI to reproduce this), it'll be much easier to help you. See [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen May 25 '19 at 18:31
  • @FrankvanPuffelen I changed it https://stackoverflow.com/questions/56320635/how-can-i-loop-over-loop-over-the-items-in-the-cart-2-add-each-item-to-the-new –  May 27 '19 at 06:25

0 Answers0