I have a list of albums (several hundred). When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No
Following solution can be used for highlighting one ore many list items. Starting point: There is a ListFragment to show a List of MyEntity via an ArrayAdapter implementation. The color of an TextView as part of the item layout has to change to grey, if the item is selected. Multiple items should be selectable.
The ListFragment must provide an OnItemSelectListener(YourEntity item) that will be called in onListItemClicked().
public void onListItemClick(ListView l, View v, int position, long id) {
MyEntity entity = (MyEntity) getListView().getItemAtPosition(position);
if (entityListSelectListener != null)
itemSelectListener.onItemSelect(entity); }
The ArrayAdapter owns a List for the key fields for the selected items and provides the manager methods addSelection(MyEntity) and clearAllSelections(). The overriden method getView() reads the key field list and, if the actual position is in there, changes the particular TextView color to grey, otherwise to transparent.
public View getView(int position, View convertView, ViewGroup parent) {
...
if (selectedEntityList.contains(entity.key)) //... highlight view field }
In onCreate() of the owning activity the listeners has to be implemented to manage the ArrayAdapter: calling addSelection() and notifyDataSetChanged().
entityListFragment.setEntityListSelectListener(
new EntityListFragment.OnItemSelectedListener() {
public void onItemSelect(MyEntity entity) {
aa.addSelection(entity);
aa.notifyDataSetChanged(); }});