Because my listView/List items have checkboxes I have the problem, that checkboxes are checked or unchecked when I’m scrolling through the list. Therefore I need to track checked items in a ArrayList for example. So I added an OnCheckedChangeListener on each checkbox and I’ve added the listener in the “getView” from the custom adapter for the listview.
Now since every checbox react on the click I have the following problem.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (!MainActivity.getSharedInstance().getIsCheckedArray().contains(tasks.get(MainActivity.ID)))
MainActivity.getSharedInstance().getIsCheckedArray().add(tasks.get(MainActivity.ID));
for (String i : MainActivity.getSharedInstance().getIsCheckedArray()) {
Log.i("RUN", i);
}
} else {
if (MainActivity.getSharedInstance().getIsCheckedArray().contains(tasks.get(MainActivity.ID)))
MainActivity.getSharedInstance().getIsCheckedArray().remove(tasks.get(MainActivity.ID));
for (String i : MainActivity.getSharedInstance().getIsCheckedArray()) {
Log.i("RUN", i);
}
}
}
});
From each list item I have an own ID and I can reach the item with that ID. But when I click on the checkboxes to get the ID, I only get the ID of one item ( the last item ). When I click on the other checkboxes they react to the change but I only get the ID from the last item and not from the items before the last one.
Do I overwrite the listeners when I always create the new Listener?
Update:
Nevermind, I added this to the onCheckChangeListener
String curIndex = tasks.get(MainActivity.ID);
and it worked
I Think your mistake is, that You doesn´t use a LayoutInflater. Just try:
this should work