There is no guarantee that any particular ListView will even have a view at any given time. If an item is currently off screen, it may not have a view. Since a particular element may not have a representation, it may not make sense to try to get a representation of the element.
In addition, due to the way the ListView creates and reuses views, you will see some odd unwanted effects if you just change the views directly. As the user scrolls through the list, the elements that become visible will incorrectly coincide with the same background as other elements that fall outside the visible part.
I don’t know if what follows is the best way to implement your functionality, because I don’t know the cost of rebuilding the list after the change. Here's a (possibly naive) way to do this:
- Add another logical element to your data object, something like
isInSecondList . - Replace
getView() in the adapter. In getView() set the background to both normal and highlighted depending on the value of the isInSecondList element. - When an item is added or removed from the second list, update the data object to reflect the change, then call the adapter
notifyDataSetChanged() .
erichamion
source share