2012-05-23 197 views
0

我有一个扩展ListActivity类,其做工精细getSelectedItemPosition()总是返回-1

然后在onListItemClick()我用getSelectedItemPosition()和它总是返回-1

PS getSelectedItemId()还有些长号码一样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(); 
    } 
} 

布局:

<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> 
+0

发布您的代码,我的水晶球今天在店里。 – Barak

+0

已经使用onListItemClick参数列表中的'final long id' –

+0

Whay不使用即将进来的'position'参数吗?无需使用该方法...信息已经存在。 – Barak

回答

9

然后在onListItemClick()我用getSelectedItemPosition()和它总是返回-1

这是因为没有被选中。 “点击”和“选择”是分开的东西。 “选择”ListView通过指点设备(D-pad,轨迹球,箭头键等)完成。