I've got a ListView with a custom BaseAdapter. The list items contain CheckBoxes that need to represent a property from a database.
I use CheckBox.setOnCheckedChangeListener with a new OnCheckedChangeListener to detect changes, so I can change the database based on the current state of the CheckBox. Pretty straightforward stuff so far.
However, when scrolling further down the list, previously checked CheckBoxes get unchecked. I suspect this happens as soon as the views are recycled (I'm using the convertView/ViewHolder technique).
How can I stop this? What's going wrong?
Thanks in advance.
Edit: To make things a bit clearer, the problem is that recycling views somehow calls OnCheckedChangeListener#onCheckedChanged(buttonView, isChecked) with isChecked == false.
Apparently the problem was that, by getting the checkbox using convertView.findViewById(), the onCheckedChangeListeners were still intact if the view was recycled. Calling checkbox.setOnCheckedChangeListener(null) did the trick.
Use a boolean array to store the checked state of each list item, record the changes inside setOnCheckedChangeListener(), then call setChecked() after setOnCheckedChangeListener().
Related
I have a listview with a custom adapter (extends BaseAdapter).
It recieves a list of objects I have to populate a ListView.
One of my object's atribute is a boolean called "checked".
on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.
Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.
But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:
"How do I know which item (object) I have to update just by checking my CheckBox?"
"Don't they have the same name?"
I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?
In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?
I'm sorry I didn't post my code, but right now I can't access it.
Take a look at this tutorial
You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)
i have list view with checkbox and if I select some item than every 9 item is selected too.
I am using onClickListener on checkbox.
Do you know how solve the situation? One item checked, but more selected.
If you wanna see my source code, just let me know and I will put there.
Thanks for help
Lukas P.
Your problem comes from views being recycled by Android. You need to store the check box state of each row in some container (eg. a list) and then explicitly set the checked state to the correct state in getView.
See this similar question:
custom ListView with checkboxes checking unchecked items
I have a problem with my customized Listview with 2 TextViews and a CheckBox in each item, the problme is when scrolling the Listview, the checked items get unchecked randomly and vice versa, can anyone help to define a customized Adapter to solve this problem, i'll be so thankful
I had the same problem and I solved as follows:
I have a boolean variable isCheckedByUser. In the adapter, everytime I do something with the checkbox, i set this variable to false first, do all the things I have to with the checkbox, and set the variable to true again. Then, in the method onCheckedChanged I check if this variable is true, if so, means that the user wanted to modify the state, so I do whatever its needed with the checkbox.
In fact, this problem is not so "random". The listview reuses the cells, so that's why for you seems like the checkboxes are unchecked randomly.
Check that you correctly recycle the convertView param in getView.
If the view was already in use, ensure the checked state fits with the new item represented.
I have a problem with checkboxes in listview.
Every raw have it's own checkbox and textview.
If i check 1st checkbox -> checked become every 5th checkbox (2-nd -> 6th)
Where is the problem?
Without some posted code it's impossible to say for sure but it sounds like an issue with View recycling in your List Adapter. If you are maintaining references to specific Views in your Activity you will have problems when the view is recycled because your reference will now point to a different view.
My problem is list of checkboxes are there which are checked in gridview , while scrolling the list, those checked one is turned to unchecked one.
Shiva,
If you are using custom Adapter for your list,then you can implement an ArrayList to keep track of the CheckBox State. And in your getView() function of Adapter, set your CheckBox true/false according to the ArrayList. This is a starting point for your problem. I hope you can figure out the rest. If you still have any problem, you can ask.