I know how to access a listview item through an onitemclicklistener but how do I do something like change the background color in code.
sudo code:
lv[0].setBackgroundResource(R.color.red); //change the background of the first listview item
Is there a way to access each view of the listview?
@Matt: It’s not that it’s weak, you just have to understand the way it works.
Yellavon, at any given time, your ListView only contains the visible items, so you can’t access and change items directly. As they scroll out of view, the views are recycled, and populated with data by the ListAdapter. It’s within this adapter that you need to handle the cases where items should differ. If you’ve created your own custom ListAdapter (e.g. ArrayAdapter, BaseAdapter), in your
getView()method, simply add some logic within to handle the cases in which the background color should change.Let’s say for example, you have a list of integers, and you want any integer >= 50 to show up in red:
(It’s important to make sure to handle the else cases, as you may get a recycled view of an item that had been colored red. In this case, you’d have to reset it to whatever default background color you need.)
If you’ve never built a custom adapter, this excerpt from CommonsWare’s book on creating custom ListAdapters is a great resource.
EDIT: Further thought, based on your comment:
In your custom ExpandableListAdapter
Implement
onListItemClick(ListView l, View v, int pos, long id)in your ListActivityNow, back in
getView()