Android ListView: how to select an item?

I am having problems with a ListView I created: I want the item to be selected when I click on it.

My code for this looks like this:

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 clicked eg. on the second element (i.e. pos = 1) I get:

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

      

i.e. even though OnItemClickListener is called with the correct argument and calls setSelection (1) the item is not selected (and hence OnItemSelectedListener.onItemSelected (...) is never called) and getSelectedItemPosition () still gives -1 after setSelection (1) -Call.

What am I missing?

Michael

PS: There are> = 2 items in my list ...

+3


source to share


3 answers


Missing item here choiceMode

. It's not very well documented , but ListViews (and in addition everything that inherits from AbsListView like GridView, etc.) is not allowed in android by default, but it can be included - either in XML format or in code :

in XML:

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

      

code:



mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

      

Note that once you've done that, android is there setSelection()

for you, so you don't need to track it yourself. At this point, your onClickListener is just to keep 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
}

      

+8


source


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 this helps you.

+2


source


I just realized that I was completely off. In touch mode, it doesn't make sense to select an item at all. You just click on it when you click (and the OnClickListener is called). I'm just starting to realize that Android will take care of all these different devices that have directional buttons, touchscreens, etc. It is not always easy to imagine how an operation is performed on a device that is not in use or cannot be tested.

+1


source







All Articles