I have posted the same problem a couple of times but it hasn't yet been resolved. I have a ListFragment
and I want to highlight the selected item in the list. I have been given suggestions to use a "selector". I don't understand how to use this selector. My ListFragment
class is:
// Create an adapter with list of stores and populate the list with
// values
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, StoreList);
setListAdapter(adapter);
mDbHelper.close();
}
/*
* (non-Javadoc)
*
* Handles the event when an item is clicked on left pane, performs action
* based on the selection in left pane
*
* @see android.app.ListFragment#onListItemClick(android.widget.ListView,
* android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String selectedStore = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
v.setBackgroundColor(getResources().getColor(R.color.BLUE));
// passes selectedStore to detail fragment
fragment.setText(selectedStore);
// getItemList(selectedStore);
}
Using setBackground sets the color permanently, but I want it to go away when another item is selected.
I understand how to use a selector in a ListView
but in my case if I haven't defined any xml for the Listview
, then how would I use a "selector"? I am using android.R.layout.simple_list_item_1
which is predefined.
I wasn't getting what I wanted so I kept on digging and came up with a "cheap" solution which might not be the best practice but does the job.
I wanted the item to be highlighted when selected and the color should go away when other item is selected from the listFragment.
This is what worked for me- I defined a static View V;
and initialized it V = new View(getActivity());
Then inside my
onListItemClick(ListView l, View v, int position, long id)
V.setBackgroundResource(0);
v.setBackgroundResource(R.color.BLUE);
V = v;