getSelectedItemPosition() always return -1

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have a class that extends ListActivity, its work fine

Then in the onListItemClick() I use getSelectedItemPosition() and its always return -1

P.S getSelectedItemId() return some long number like 994393434

public class TasksShowActivity  extends ListActivity {      private Cursor mCursor;      private ListAdapter mAdapter;      private static final String[] mContent = new String[] {         TasksDbHelper._ID, TasksDbHelper.NAME,         TasksDbHelper.USER};      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);               mCursor = managedQuery(                 TasksProvider.CONTENT_URI, mContent, null, null, null);          mAdapter = new SimpleCursorAdapter(this,                      R.layout.tasks, mCursor,                      new String[] {TasksDbHelper.NAME, TasksDbHelper.USER},                      new int[] {R.id.name, R.id.date});          setListAdapter(mAdapter);     }      @Override     protected void onListItemClick(ListView l, View v, int position, final long id) {         super.onListItemClick(l, v, position, id);        Toast toast = Toast.makeText(this, "Position: "+this.getSelectedItemPosition() , Toast.LENGTH_SHORT);             toast.setGravity(Gravity.CENTER, 0, 0);             toast.show();     } } 

layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:id="@+id/tasks_root_element">      <TextView         android:id="@+id/name"         android:layout_width="wrap_content"          android:layout_height="wrap_content"         android:layout_alignParentLeft="true"          android:textSize="18sp"         />     <TextView         android:id="@+id/date"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentRight="true"         android:textSize="18sp"          android:paddingRight="10px"         />  </RelativeLayout> 

回答1:

Then in the onListItemClick() I use getSelectedItemPosition() and its always return -1

That is because nothing is selected. "Click" and "select" are separate things. "Select" in a ListView is done via the pointing device (D-pad, trackball, arrow keys, etc.).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!