Search code examples
androidlistviewonfocus

How to add Focuschange Listner to listview in android


I am having a listview in one of my activity. How to set focus change listener. I tried with following the block of code I am not getting a result.

product_search_list = (ListView) findViewById(R.id.productlist);
product_search_list.setOnFocusChangeListener(onproductchangelistener);

private OnFocusChangeListener onproductchangelistener = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int index= (int) ((ListView)v).getSelectedItemId();
        Log.i("index",""+index);
    }
};

Actually, on focus change of list items, I want to get the index of the focused list item and do the necessary operations.

Thanks in advance.


Solution

  • You probably want setOnItemSelectedListener(), not setOnFocusChangeListener().