title: ListView setOnItemClickListener and setOnItemSelectedListener to store the Selected Item Index
body: <p>I have read on this site that it is necessary to customize the setOnItemSelectedListener and setOnItemClickListener of a ListView if we want to know the Index of the SelectedItem (.getSelectedItemPosition()). So that is what I do but it does not stores the position of the SekectedItem, instead i have always -1...</p>
  
  <p>What I want to do is just to give the user a way to delete items from a list by selected and Item and Clicking a button.</p>
  
  <p>See the code below :</p>
  
  <pre><code>    listViewPeople.setOnItemClickListener(new ListView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; a, View v, int i, long l) {
              try {
                  // Remembers the selected Index
                  listViewPeopleId = listViewPeople.getSelectedItemPosition();
              }
              catch(Exception e) {
                  System.out.println("Nay, cannot get the selected index");
              }
          }
      });
  
      listViewPeople.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; a, View v, int i, long l) {
              try {
                  // Remembers the selected Index
                  listViewPeopleId = listViewPeople.getSelectedItemPosition();
                  System.out.println("Yay, set the selected index " + listViewPeopleId);
              }
              catch(Exception e) {
                  System.out.println("Nay, cannot get the selected index " + listViewPeopleId);
              }
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
              try {
                  // Remembers nothing selected
                  listViewPeopleId = -1;
                  System.out.println("Yay, set that nothing is selected " + listViewPeopleId);
              }
              catch(Exception e) {
                  System.out.println("Nay, cannot set that nothing is selected " + listViewPeopleId);
              }
          }
      });
  </code></pre>
  
  <p>What's wrong??</p>
  
  <p>Thank you for your help!</p>
  
  <p>Christophe</p>