Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: List View Selected item -1

Tags:

android

Im getting a -1 value when i try to get the selected item position on my listview 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 fill my list view 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 Regards.

Jose.

like image 754
Sosi Avatar asked Mar 12 '10 16:03

Sosi


1 Answers

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

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

And INVALID_POSITION is -1.

Note that you are calling getSelectedItemPosition() from an OnClickListener. Click and selection are not necessarily related. Selection comes from using the D-pad or trackball to navigate the list contents. If the user taps on the screen (or clicks in the emulator), there will be no selection anymore, but there will still be a click event.

The arg2 value you show is the position of the clicked-upon item in the list.

like image 148
CommonsWare Avatar answered Nov 02 '22 09:11

CommonsWare