title: Android Listview OnItemClickListener
body: <p>I am starting to learn how to make use of the ListView for user selection based on the following tutorial 
  <a href="http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/" rel="nofollow noreferrer">http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/</a> 
  , but i need to extend its function to allow mobile user to click on the list and return the value on the edittext. No records should be viewed in the ListView. The textstring in edittext will be used to conduct the SQL search in SQLITE database.</p>
  
  <p>I have got similar question asked by the Agustín in the post:
  <a href="https://stackoverflow.com/questions/21295328/android-listview-with-onclick-items">Android ListView with onClick items</a>
  but i find it difficult to rewrite the code successfully based on the suggested answers. </p>
  
  <pre><code>package com.example.listviewtesting;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.EditText;
  import android.widget.ListView;
  
  public class MainActivity extends Activity {
  
  // List view
  private ListView lv;
  
  // Listview Adapter
  ArrayAdapter&lt;String&gt; adapter;
  
  // Search EditText
  EditText inputSearch;
  
  
  // ArrayList for Listview
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; productList;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Listview Data
      String products[] = {"Anita Chan Lai Ling Building",
              "Auxiliary Teaching Block",
              "BC Wing",
              "Block L",
              "Block Z",
              "CD Wing",
              "Chan Sui Wai Building",
              "Chan Tai Ho Building",
              "Cheung On Tak Lecture Theatre",
              "Choi Kai Yau Building",
              "Chow Yei Ching Building",
              "Chung Sze Yuen Building",
              "Communal Building",
              "Core A",
              "Core B",
              "Core C",
              "Core D",
              "Core E",
              "Core F",
              "Core G",
              "Core H",
              "Core J",
              "Core P",
              "Core Q",
              "Core R",
              "Core S",
              "Core T",
              "Core U",
              "Fong Shu Chuen Hall",
              "GH Podium Annexe",
              "GH Wing",
              "House of Innovation",
              "Jockey Club Auditorium",
              "Jockey Club Innovation Tower",
              "Kinmay W.Tang Building",
              "Lee Shau Kee Building",
              "Li Ka Shing Tower",
              "Lui Che Woo Building",
              "Mong Man Wai Building",
              "Ng Wing Hong Building",
              "Pao Yue Kong Library",
              "Realink Building",
              "Shaw Amenities Building",
              "Shaw Sports Complex",
              "Shirley Chan Building",
              "Stanley Ho Building",
              "Tang Ping Yuan Building",
              "Yip Kit Chuen Building"};
  
      lv = (ListView) findViewById(R.id.list_view);
      inputSearch = (EditText) findViewById(R.id.inputSearch);
  
      // Adding items to listview
  
      adapter = new ArrayAdapter(this, R.layout.list_item, R.id.product_name, products); 
      lv.setAdapter(adapter);
  
  
      /**
       * Enabling Search Filter
       * */
  
      lv.setOnItemClickListener(new OnItemClickListener(){
  
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt;adapter,View v, int position){   
          String selectedProduct = products[position];
          inputSearch.setText(selectedProduct);
          }
  
  
          });
  
      inputSearch.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
              // When user changed the Text
              MainActivity.this.adapter.getFilter().filter(cs);   
          }
  
          @Override
          public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                  int arg3) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void afterTextChanged(Editable arg0) {
              // TODO Auto-generated method stub                          
          }
      });
  }    
  }
  </code></pre>