I’m working on an e-commerce app for Android using Kotlin. Right now I can show products on the home screen after getting data from an API. Each product has its own detail screen where users can add it to the cart with a specific quantity. But I’m stuck on how to add different products with different quantities to the cart.
Currently, I can add one product, but the quantity is the same for all products when clicked. I want to display each product on separate cards in the cart screen.
Here’s what I’ve done so far:
- Created a ProductDetailFragment for each product
- Set up a MainViewModel to handle data across fragments
- Implemented basic add to cart functionality
I’m not sure how to:
- Keep track of multiple products and their quantities
- Update the cart when a new product is added or quantity changes
- Display different products on separate cards in the cart
Has anyone dealt with this before? Any tips or code examples would be super helpful. Thanks!
Hey there, fellow app developer! 
I’ve been down this e-commerce rabbit hole before, and let me tell you, it’s quite the adventure! Your multi-item cart challenge sounds familiar. Have you considered using a HashMap to keep track of your products? It’s super handy for quick lookups and updates.
Something like this might work:
private val cartItems = HashMap<Int, CartItem>()
This way, you can easily check if a product exists, update its quantity, or add a new one.
For displaying the items, RecyclerView is definitely your friend here. But here’s a thought - what if you made your cart items expandable? Like, tap to see more details or adjust quantity? Could be a cool user experience!
Oh, and don’t forget about persistence! Are you saving the cart state when the app closes? SharedPreferences could be a quick solution, or Room if you’re feeling fancy.
Curious to hear how you end up tackling this. What’s been the trickiest part so far? Any cool features you’re planning to add to your cart screen?
I’ve dealt with similar cart functionality in my e-commerce projects. For tracking multiple products and quantities, I recommend using a MutableStateFlow in your ViewModel:
private val _cartItems = MutableStateFlow<List<CartItem>>(emptyList())
val cartItems: StateFlow<List<CartItem>> = _cartItems.asStateFlow()
This approach provides better performance and reactivity compared to LiveData.
To update the cart, create functions in your ViewModel to add/update items:
fun addToCart(product: Product, quantity: Int) {
val updatedCart = _cartItems.value.toMutableList()
val existingItem = updatedCart.find { it.productId == product.id }
if (existingItem != null) {
existingItem.quantity += quantity
} else {
updatedCart.add(CartItem(product.id, product.name, product.price, quantity))
}
_cartItems.value = updatedCart
}
For displaying items, use a LazyColumn with Jetpack Compose or RecyclerView with a custom adapter in XML layouts. This allows efficient rendering of multiple product cards.
Hope this helps with your implementation. Let me know if you need any clarification on these concepts.
hey mate, been there done that!
for tracking products n quantities, try using a MutableList of CartItems in ur ViewModel. smthn like:
private val _cartItems = MutableLiveData<MutableList>()
Then u can add/update items easily. For displaying, RecyclerView is ur best bet. Create a custom adapter to show each CartItem on a separate card.
hope this helps! let me know if u need more tips