Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

android - Why is my onItemSelectedListener not called in a ListView?

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"
    android:choiceMode="singleChoice">
</ListView>

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

getListView().setAdapter(adapter);
getListView().setOnItemSelectedListener(this);

my Activity implements the listener like that:

@Override
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...