I have a ListView in a PopupWindow. The PopupWindow is initialized like this
window.setContentView(root);
wind
I finally used a custom adapter to store the selected value and use it from there to mark it:
public class FileExplorerAdapter extends ArrayAdapter {
/** File names */
private List values = new ArrayList();
/** Currently selected position */
private int selected = -1;
public FileExplorerAdapter(Context context, List values) {
super(context, R.layout.explorer_row, values);
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I know that my layout is always a TextView
TextView row = (TextView) convertView;
if (row == null) {
row = (TextView) ViewHelper.inflateViewById(getContext(),
R.layout.explorer_row);
}
// More code...
// Set up the background for selected element
if (selected == position) {
row.setBackgroundColor(Color.LTGRAY);
// Override background selector
} else {
row.setBackgroundColor(Color.TRANSPARENT);
}
// More code...
return row;
}
/** This sets the selected position */
public void setSelected(int position) {
selected = position;
}
}
And on the class that implements the OnItemClickListener for the associated ListView, I set up the currently selected item in the adapter.
@Override
public void onItemClick(AdapterView> adapter, View v, int pos, long id) {
FileExplorerAdapter fileadapter = (FileExplorerAdapter) fileList
.getAdapter();
fileadapter.setSelected(pos);
}