tags:

views:

525

answers:

1

Hi,

I have trying to set an OnKeyListener, but I am not sure how can I get the selected row on the list view in the OnKeyListener(). The 'v' parameter always give 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) {
             //....
            }
}
+3  A: 

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.

Bakhtiyor