Why is my onItemSelectedListener not called in a ListView?

AndroidAndroid ListviewListener

Android Problem Overview

I'm using a ListView that is setup like this:

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:longClickable="false"

In my code I add an OnItemSelectedListener to the ListView like this:


my Activity implements the listener like that:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Log.d("Tag", "ListItemSelected: Parent: " + parent.toString() + " View: "
            + view.toString() + " Position: " + " Id: " + id);

My hope was, that I would see this debug output the moment I click on something in the list. But the debug output is never shown in LogCat.

Android Solutions

Solution 1 - 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.

Solution 2 - Android

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.

Solution 3 - Android

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

enter image description here

you can get the seletedItem by this method in setOnItemClickListener

     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        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());



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJanuszView Question on Stackoverflow
Solution 1 - AndroidJanuszView Answer on Stackoverflow
Solution 2 - AndroidEdward BreyView Answer on Stackoverflow
Solution 3 - AndroidjoeView Answer on Stackoverflow