I have a ListView in a PopupWindow. The PopupWindow is initialized like this
window.setContentView(root);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Then the ListView:
fileList = (ListView) root.findViewById(R.id.explorer_list);
fileList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
fileList.setSelector(android.R.drawable.screen_background_light_transparent);
fileList.setOnItemClickListener(this);
[...]
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
selected = (File) fileList.getItemAtPosition(pos);
}
Like this, everything works correctly except that the selector will not show up on selection until ListView is scrolled (nothing shows visually as selected until the list is scrolled, although the item is correctly selected).
If I set the PopupWindow not focusable, then the visual selection works correctly (the item is visually selected right when clicked into) but onItemClick() is never called and thus I cannot get the selected item.
ListView.getSelectedItem() always returns null in both cases, even if there’s a selected item.
Any idea on how to solve this situation? Thanks in advance.
I finally used a custom adapter to store the selected value and use it from there to mark it:
And on the class that implements the
OnItemClickListenerfor the associatedListView, I set up the currently selected item in the adapter.