I've done it manually in the following manner before:
Create an array to hold the selected state of the list, initialize it in the adapter constructor, then reference that in your getView method (in case the currently selected item scrolls out of view) and your onItemClick method (to change the current selection and turn off the old one).
public static boolean selectedStatus[]; // array to hold selected state
public static View oldView; // view to hold so we can set background back to normal after
Constructor initialize the array
public class MyCursorAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private int layout;
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mInflater = LayoutInflater.from(context);
this.layout = layout;
selectedStatus = new boolean[c.getCount()];
for (int i = 0; i < c.getCount(); i++) {
selectedStatus[i] = false; // Start with all items unselected
}
}
}
getView needed for when the child scrolls out of view
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = View.inflate(context, layout, null);
if(selectedStatus[position] == true){
v.setBackgroundResource(R.color.blue);
} else {
v.setBackgroundResource(R.color.black);
}
return v;
}
onItemClick change selected item in array and on screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mRowId = id;
EventDisplayFragment eventdisplay = new EventDisplayFragment();
getFragmentManager().beginTransaction()
.replace(R.id.rightpane, eventdisplay).commit();
if(MyCursorAdapter.oldView != null){
MyCursorAdapter.oldView.setBackgroundResource(R.color.black); // change the background of the old selected item back to default black }
MyCursorAdapter.oldView = v; // set oldView to current view so we have a reference to change back on next selection
for (int i = 0; i < selectedStatus.length; i++) {
if(i == position){ // set the current view to true and all others to false
selectedStatus[i] = true;
} else {
selectedStatus[i] = false;
}
}
}
v.setBackgroundResource(R.color.blue);
return true;
}
});