Android: view list Selected item -1

Im getting a value of -1 when I try to get the position of the selected item in my list that is already populated.

list.setOnItemClickListener ( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView adapterView, View view,int arg2, long arg3) { int selectedPosition = adapterView.getSelectedItemPosition(); ShowAlert(String.valueOf(selectedPosition)); } } ); 

To populate my list, I use the following code:

 SimpleAdapter mSchedule = new SimpleAdapter( this, mylist, R.layout.listviewtest, new String[] {"test1", "test2", "test3"}, new int[] {R.id.TextView_websitename, R.id.TextView_keywords, R.id.TextView_backlink}); 

Any idea?

Thanks in advance.

Best wishes.

Jose.

+6
android
source share
1 answer

This means that no row is selected. The documentation states that getSelectedItemPosition() returns:

int Position (starting at 0) or INVALID_POSITION if nothing is selected.

And INVALID_POSITION -1 .

Note that you are calling getSelectedItemPosition() from OnClickListener . Click and selection are not necessarily related. The selection is carried out using the D-pad or trackball to navigate through the contents of the list. If the user clicks on the screen (or clicks in the emulator), there will be no more choice, but there will still be a click event.

The arg2 value you are showing is the position of the item with clicking on the item in the list.

+22
source share

All Articles