title: Android ListView: how to select an item?
body: <p>I am having trouble with a ListView I created: I want an item to get selected when I click on it.</p>
  
  <p>My code for this looks like:</p>
  
  <pre><code>protected void onResume() {
  ...
  ListView lv = getListView(); 
  lv.setOnItemSelectedListener(new OnItemSelectedListener() 
  {
      public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int pos, long id) {
          Log.v(TAG, "onItemSelected(..., " + pos + ",...) =&gt; selected: " + getSelectedItemPosition());
      }
      public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {
          Log.v(TAG, "onNothingSelected(...) =&gt; selected: " + getSelectedItemPosition());
      }
  });
  lv.setOnItemClickListener(new OnItemClickListener()
  {
      public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int pos, long id) {
          lv.setSelection(pos);               
          Log.v(TAG, "onItemClick(..., " + pos + ",...) =&gt; selected: " + getSelectedItemPosition());              
      }
  });
  ...
  }
  </code></pre>
  
  <p>When I run this and click e.g. on the second item (i.e. pos=1) I get:</p>
  
  <pre><code>04-03 23:08:36.994: V/DisplayLists(663): onItemClick(..., 1,...) =&gt; selected: -1
  </code></pre>
  
  <p>i.e. even though the OnItemClickListener is called  with the proper argument and calls a setSelection(1), there is no item selected (and hence also OnItemSelectedListener.onItemSelected(...) is never called) and getSelectedItemPosition() still yields -1 after the setSelection(1)-call.</p>
  
  <p>What am I missing?</p>
  
  <p>Michael</p>
  
  <p>PS.: My list <strong>does</strong> have >=2 elements...</p>