android listview getselectedItem returns null
Asked Answered
B

1

7

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.

Beadle answered 1/3, 2015 at 21:15 Comment(0)
H
11

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);
Hemoglobin answered 16/11, 2016 at 14:14 Comment(1)
this is an old question but I am baffled on how android actually sucksRy

© 2022 - 2024 — McMap. All rights reserved.