Android ListView: how to select an item?

Go To StackoverFlow.com

3

I am having trouble with a ListView I created: I want an item to get selected when I click on it.

My code for this looks like:

protected void onResume() {
...
ListView lv = getListView(); 
lv.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
        Log.v(TAG, "onItemSelected(..., " + pos + ",...) => selected: " + getSelectedItemPosition());
    }
    public void onNothingSelected(AdapterView<?> adapterView) {
        Log.v(TAG, "onNothingSelected(...) => selected: " + getSelectedItemPosition());
    }
});
lv.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
        lv.setSelection(pos);               
        Log.v(TAG, "onItemClick(..., " + pos + ",...) => selected: " + getSelectedItemPosition());              
    }
});
...
}

When I run this and click e.g. on the second item (i.e. pos=1) I get:

04-03 23:08:36.994: V/DisplayLists(663): onItemClick(..., 1,...) => selected: -1

i.e. even though the OnItemClickListener is called with the proper argument and calls a setSelection(1), there is no item selected (and hence also OnItemSelectedListener.onItemSelected(...) is never called) and getSelectedItemPosition() still yields -1 after the setSelection(1)-call.

What am I missing?

Michael

PS.: My list does have >=2 elements...

2012-04-03 23:17
by mmo


8

The missing element here is choiceMode. This isn't terribly well documented, but ListViews (and by extension, anything that inherits from AbsListView, like GridView, etc.) in android by default do not allow for selection, but it can be enabled - either in XML or in code:

in XML:

<ListView
  ...
  android:choiceMode="singleChoice" />

Code:

mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

Note that once you do this, android will setSelection() for you, so you don't need to keep track of it yourself. At that point your onClickListener is just for saving the selection and I don't even bother with the OnSelectedItemListener :

@Override
public void onItemClick(final AdapterView<?> list, final View v,
    final int position, final long id) {
  Participant p = mAdapter.getParticipantForId(id);
  eventManager.fire(new ParticipantSelectedEvent(p));
  pxList.smoothScrollToPosition(position); // Make sure selection is plainly visible
}
2012-04-03 23:25
by JRaymond
Why was this downvoted, exactly? It solves the posters problem of getSelectedItemPosition() never changing. Yes, if all he wants is the item in question, then getItemAtPosition in onItemClick() is certainly easier. choiceMode, however, is there for being able to show the user which item is currently selected, as well as giving the programmer access to it - JRaymond 2012-04-04 15:08
I added the choice mode as described (in both variants) but no change! The OnItemClickListener still does not get called. And that's even though I get a reaction on-screen, i.e. the item shortly turns purple and then back to white again. Strange enough, I can select items by hitting the up and down buttons (and then the OnIemSelectListener gets called) but obviously not via the OnItemClickListener - mmo 2012-04-04 20:55
Wait, so OnItemClick never gets called?? OnItemSelect listener is configured only for the trackPad/up down stuff - It doesn't respond to clicks and I don't know whether or not it will respond to a manual call of setSelectio - JRaymond 2012-04-04 21:01
Because you're working on a phone with hybrid controls, you may have to have both the onItemSelected() call and the onClick() call point to the same function (of your defining) if you want the same functionality for both events. There are quirks involved with working in touch mode vs non-touch mode that I honestly don't know much about - JRaymond 2012-04-04 21:09
It's even weirder: OnItemClick does get called (with the correct position). It calls setSelection(pos), which gets ignored (i.e. it has no effect whatsoever. After that call the selectedItemPosition is still -1) - mmo 2012-04-04 22:01
To test setSelection() I added two menu options calling setSelection programmatically. The result is really strange to me: first onItemSelected() is called and the item is selected, but immediately after onNothingSelected() is called, i.e. the items gets immediately deselected again. I simply don't get the logic behind this!

All I want to achieve is that when a user clicks on or rather touches an item (= clicks on it with the mouse in the emulator), that the item gets selected and correspondingly highlighted as being selected - mmo 2012-04-04 22:11



2

Try this:--

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item
          String product = ((TextView) view).getText().toString();

         Toast.makeText(getApplicationContext(), "Selected Item :" +product, Toast.LENGTH_SHORT).show();

      }
    });

Hope it will help you.

2012-04-04 05:19
by Hulk


1

I just realized, that I was completely off. In touch-mode, it doesn't make sense at all, to select an item. You just directly operate on it, when clicked (and the OnClickListener is called). I am just starting to appreciate, that Android takes care of all these different devices, that have direction control buttons, touch screens, etc. It's just not always easy to imagine, how an operation is done on a device that one isn't used to or can't test on.

2012-04-04 23:26
by mmo
Ads