arash shahzamani
arash shahzamani

Reputation: 87

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.

Upvotes: 7

Views: 2139

Answers (1)

vladimir123
vladimir123

Reputation: 494

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);

Upvotes: 11

Related Questions