Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use this.getSelectedItemId() in Android
    primarykey
    data
    text
    <p>I start learning android just now with sample examples. I encountered one problem. </p> <pre><code>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&lt;?&gt; 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 &gt; 0) { CallEditContactDialog(id); } else { Toast.makeText(this,R.string.toast_notify,Toast.LENGTH_SHORT).show(); } break; case IDM_DELETE: if (id &gt; 0) { CallDeleteContactDialog(id); } else { Toast.makeText(this, R.string.toast_notify, Toast.LENGTH_SHORT).show(); } break; } return super.onOptionsItemSelected(item); } } </code></pre> <p>It has been written here - <code>this.getSelectedItemId()</code> , and the code returned <a href="http://developer.android.com/reference/android/widget/AdapterView.html#INVALID_ROW_ID" rel="nofollow">INVALID_ROW_ID</a>, I read its <a href="http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemId%28%29" rel="nofollow">documentation</a>, 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!</p> <p><strong>EDIT:</strong></p> <p>My xml file like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textSize="18sp"/&gt; &lt;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"/&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload