Reputation: 20341
I have been trying to set an OnKeyListener
, but I am not sure how I can get the selected row on the ListView
in the View.OnKeyListener
. The v
parameter always gives me the ListView
, not the selected row. Any idea how to solve this problem? Thank you.
listView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
//....
}
}
Upvotes: 3
Views: 5602
Reputation: 4376
You can use AbsListView.getSelectedView() method:
public boolean onKey(View v, int keyCode, KeyEvent event) {
ListView listView = (ListView) v;
if (listView.getSelectedView() != null) {
// (cast if necessary) and use selected view
}
}
If you interested in selected item position, id or associated object you can use AdapterView.getSelectedItemPosition(), AdapterView.getSelectedItemId() and AdapterView.getSelectedItem() methods correspondingly.
Upvotes: 8