Reputation: 2832
I start learning android just now with sample examples. I encountered one problem.
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);
}
}
It has been written here - this.getSelectedItemId()
, and the code returned INVALID_ROW_ID, I read its documentation, but I didn't find any way to resolve it. What do I need to add to resolve the problem ? What does this.getSelectedItemId() returned , in general? Thanks in advance!
EDIT:
My xml file like this:
<?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>
Upvotes: 1
Views: 2259
Reputation: 18151
When you select an item in a listview getSelectedItemId ()
returns the _id of the selected item or INVALID_ROW_ID if no item is selected.
You need to implement OnItemClickListener
and in onItemClick
call setSelection(position)
then your code will work.
Upvotes: 2
Reputation: 985
edit:
You're using the wrong method; for a ListView, you need to set an OnClickListener
. This method is for the activity's menu.
Use item.getItemId()
instead. Here's an example:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.menu.do_stuff: {
// Do stuff
} case R.menu.do_stuff2: {
// Do other stuff
}
}
}
Upvotes: 1