ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView.
Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.
The OnItemSelectedListener
listens for list item selections and not list item clicks.
A selection in this case could be seen as moving the focus on this item with the device's trackpad.
To get the wanted behavior one must use the OnItemClickListener
.
It's because you happen to be testing with your fingers on a touch-enabled device. In touch mode, there is no focus and no selection. UIs that need selection should use a different type of widget, such as radio buttons.
At first,you should set ChoiceMode
,and then,in ListView,there will not accept the selected event because setOnItemSelectedListener
registed in AdapterView
,and callback in method handleDataChanged()
,but class AbsListView
override this method and never callback OnItemSelectedListener
you can get the seletedItem by this method in setOnItemClickListener
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("TAG", "onItemClick: " + position);
SparseBooleanArray positions = mListView.getCheckedItemPositions();
Log.e("TAG", "onItemSelected: " + positions.toString());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With