我现在开始通过示例示例学习 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,我阅读了它的文档,但我没有找到任何解决它的方法。我需要添加什么来解决问题?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>