How to implement multi-item cart functionality in an Android e-commerce app using Kotlin?

I’m working on an e-commerce app for Android using Kotlin. I’ve set up the home screen to show products from an API. Users can view product details and add items to their cart with a specific quantity.

I want to allow users to add multiple products to the cart, each with its own quantity. The cart should display each product on a separate card. Right now, I can only add one product type, and the quantity applies to all products.

Here’s what I’ve done so far:

  • Created a ProductDetailFragment for individual product views
  • Set up a MainViewModel to handle data across fragments
  • Implemented basic add-to-cart functionality

I’m stuck on how to manage multiple product types and quantities in the cart. Any suggestions on how to structure this? Should I use a list of custom objects? How can I update the UI to reflect these changes?

I’m new to Android development, so any tips or code examples would be really helpful. Thanks!

For implementing a multi-item cart, you’ll want to create a CartItem data class to represent each product in the cart. This class should include properties like productId, quantity, and price. Then, use a List in your MainViewModel to store the cart contents.

To add items, create a method in your ViewModel like addToCart(product: Product, quantity: Int). This method should check if the product is already in the cart and update the quantity if it is, or add a new CartItem if it’s not.

For the UI, use a RecyclerView in your cart fragment to display the list of CartItems. Create a custom adapter and layout for each cart item.

To persist the cart between app sessions, consider using Room database or SharedPreferences.

Remember to update your UI whenever the cart changes. You can use LiveData or StateFlow in your ViewModel to notify observers of changes to the cart list.

yo, i’ve been there too! for multiple items, try using a MutableList in ur ViewModel. each CartItem can hold productId, quantity, etc. when adding to cart, check if item exists and update quantity or add new.

for UI, RecyclerView’s great. make a custom adapter for CartItems. Use DiffUtil for smooth updates.

don’t forget to update UI when cart changes. LiveData or Flow can help with that.

good luck with ur app!

Hey there! As a fellow Android dev, I totally get your struggle with the cart functionality. It can be a bit tricky at first, right?

Have you considered using a HashMap to store your cart items? Something like Map<ProductId, CartItem> could work well. This way, you can easily check if a product is already in the cart and update its quantity.

For the UI part, RecyclerView is definitely your friend here. But here’s a thought - what about adding some fun animations when items are added or removed from the cart? It could make the user experience more engaging.

Oh, and speaking of user experience, have you thought about adding a ‘Quick Add’ feature from the product list itself? Might save users a few taps.

I’m curious, how are you handling network calls in your app? Are you using Retrofit or something else? It’d be interesting to know how you’re managing the API interactions.

Keep us posted on how it goes! It’s always exciting to see how these projects evolve. Good luck!