0

I run this code blow , and the setOnItemClickListener() run well , but the selected event can not be triggered. Is there something wrong or the ListView can't bind setOnItemSelectedListener? Could somebody please help me?

public class MainActivity extends Activity{
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.list);
    ArrayAdapter aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items);
    listView.setAdapter(aa);
    listView.setChoiceMode(CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selection.setText(items[position]);
        }
    });
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this,items[position],Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    selection=(TextView)findViewById(R.id.selection);
  }
}
litbear
  • 806
  • 1
  • 6
  • 17
  • remove `setOnItemSelectedListener` and see once – Manohar Feb 18 '17 at 13:09
  • @Redman it's worked ,and trigger the click event,but I just want to trigger the selected event – litbear Feb 18 '17 at 13:13
  • I don't think you want the onItemSelected listener. The normal click listener prevents that from happening. http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/ – OneCricketeer Feb 18 '17 at 13:15
  • it will only trigger when you click an item in list view, you can know which item was clicked by using position as you already did,Keep the Toast from onitemselected to onitemclicked, i think you can see the string in toast – Manohar Feb 18 '17 at 13:19
  • @cricket_007 but when I remove `setOnItemClickListener()` the selected event was not triggered also ; I'm very confused although it's just for practise . – litbear Feb 18 '17 at 13:22
  • @Redman My question is about `setOnItemSelectedListener`,and the click event is just for comparison. I tried remove click listener ,and the selected event not run also. – litbear Feb 18 '17 at 13:26
  • 1
    You keep the click event and you can get your selected items from that (see the link). You do not need the "selected" listener – OneCricketeer Feb 18 '17 at 13:27
  • @cricket_007 I got your point, and I know i'm chasing dead ends,thank you all the same – litbear Feb 18 '17 at 13:34

1 Answers1

0

I guess you are taking about retaining the selected item as it is, if so then add the following lines in your xml list view part

android:choiceMode="singleChoice"
 android:listSelector="#666666" 

your activity should be some thing like this

public class MainActivity extends Activity{
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.list);
    selection=(TextView)findViewById(R.id.selection);
    ArrayAdapter aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items);
    listView.setAdapter(aa);
    listView.setChoiceMode(CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selection.setText(items[position]);
              Toast.makeText(MainActivity.this,items[position],Toast.LENGTH_SHORT).show();
        }
    });

  }
}

you may also follow other methods over here

Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • the `android:choiceMode="singleChoice"` will conflict to `listView.setChoiceMode(CHOICE_MODE_MULTIPLE)` ,and my key point is "why `setOnItemSelectedListener()` not be invoked even I deleted clicked listener" – litbear Feb 18 '17 at 13:48