Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android listview getselectedItem returns null

I have a listview and a button outside listview. I want the user to select an item in listview and then when clicking on button I wanna get the selected item of list view. I have set choiceMode to single choice on listview, but when I try getselecteditem it returns null. How should I get the selected Item?

thanks.

like image 861
arash shahzamani Avatar asked Mar 01 '15 21:03

arash shahzamani


1 Answers

This is an old question, so probably not that relevant anymore. But here's what's the problem:

The ListView doesn't store selected item position when you just set the choice mode. It stores the checked item position.

In short, you would use something like:

int pos = listView.getCheckedItemPosition();
myObject = (MyObject) listView.getAdapter().getItem(pos);
like image 55
vladimir123 Avatar answered Jan 02 '23 22:01

vladimir123