Skip to content
Advertisement

(Dynamic layout) Button listener in my view update multiple textview instead of 1

I’m trying android for 2 weeks and I run into some problem. I have a list of burger that I fetch from my database, I made a view with that. The app needs to increment and decrement the quantity that the user wants so that he can pass an order. enter image description here My problem is that when I click on the + button, the app update multiple text view and I don’t understand where that came from and how to fix it.

here is a sample of my code for the view :

JavaScript

Advertisement

Answer

You are not supposed to alter views of an AdapterView (ListView, Spinner, RecyclerView etc) inside methods that do not belong to Adapter classes (as you do with your onClick() method overrides. What you should be doing is after you mutate your data model notify the Adapter with .notifyDataSetChanged().

Also I strongly suggest that you do not capture the local variable Burger inside the onClick(). Rather, fetch the data from burgersList with adapter position. So your code be like:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement