android - 我如何在 Android 中使用 this.getSelectedItemId()

标签 android simplecursoradapter android-cursoradapter

我刚刚开始通过示例学习 android。我遇到了一个问题。

public class ContactActivity extends ListActivity implements OnItemClickListener {
    private Cursor mCursor;
    private ListAdapter mAdapter;
    private static final String[] mContent = new String[]{
       ContactDbHelper._ID,ContactDbHelper.NAME,ContactDbHelper.PHONE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mCursor = managedQuery(ContactProvider.CONTENT_URI, mContent, null, null, null);
         mAdapter = new SimpleCursorAdapter(this, R.layout.main, mCursor, 
             new String[]{ ContactDbHelper.NAME, ContactDbHelper.PHONE },
             new int[]{ R.id.name, R.id.phone } );
        setListAdapter(mAdapter);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
        Log.i("position: ", "" + position);
        Log.i("id", "" + id);
        setSelection(position);
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        long id = this.getSelectedItemId();
        Log.i("id: ", "" + id);
        switch(item.getItemId()) {
            case IDM_ADD: { CallAddContactDialog(); } break;
            case IDM_EDIT: if(id > 0) { CallEditContactDialog(id); } 
                  else {
                        Toast.makeText(this,R.string.toast_notify,Toast.LENGTH_SHORT).show();
                  } break;
             case IDM_DELETE: if (id > 0) { CallDeleteContactDialog(id); } else {
                       Toast.makeText(this, R.string.toast_notify, Toast.LENGTH_SHORT).show();
                   } break;
        }               
    return super.onOptionsItemSelected(item);
    }
}

这里已经写好了- this.getSelectedItemId() ,代码返回INVALID_ROW_ID ,我读了它的 documentation ,但我没有找到解决它的方法。我需要添加什么来解决问题?一般来说,this.getSelectedItemId() 返回了什么?提前致谢!

编辑:

我的 xml 文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <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/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" 
        android:textSize="18sp"
        android:paddingRight="10dp"/>
</RelativeLayout>

最佳答案

当您在 ListView 中选择一个项目时,getSelectedItemId () 返回所选项目的 _id,如果未选择任何项目,则返回 INVALID_ROW_ID。
您需要实现 OnItemClickListener 并在 onItemClick 中调用 setSelection(position) 然后您的代码将起作用。

关于android - 我如何在 Android 中使用 this.getSelectedItemId(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911748/

相关文章:

java - 如何将当前时间与Android中存储在数据库中的时间进行比较

android - 使用 SimpleCursorAdapter 的 AlertDialog 不显示值

android - ListView SimpleCursorAdapter 更新

Android - 注销适配器中定义的 ContentObserver

android - 分页列表和 CursorLoader

java - 实时检测EditText是否为空

java - Joda 日期时间 DateTimeZone

android - 安装apk时,如何使apk在sdcard上创建文件?

android - 创建自定义简单游标适配器

android - CursorAdapter 和按钮