title: Adding searchbox to listview
body: <ul>
  <li><img src="https://i.stack.imgur.com/q1Ba4.png" alt="List item"></li>
  </ul>
  
  <p>I am trying to add searchbox..</p>
  
  <p>I know the searchbox code as written below.but I am in confusion how to add and where to put..MainActvity class doesn't have access to listview as it is created in CountryXmlParser and CountryXmlParser class does not extends Actvity?I am posting code for MainActvity and CountryXmlParser</p>
  
  <p>SearchBox code</p>
  
  <pre><code>searchBox.addTextChangedListener(new TextWatcher() {
  
           public void onTextChanged(CharSequence s, int start, int before, int count) {
                 //get the text in the EditText
                 String searchString=searchBox.getText().toString();
                 int textLength=searchString.length();
  
                        //clear the initial data &lt;span class="h9umc81m835" id="h9umc81m835_8"&gt;set&lt;/span&gt;
                        list.clear();
  
                 for(int i=0;i&lt;originalValues.size();i++)
                 {
                String countryName=originalValues.get(i).get("country").toString();
                if(textLength&lt;=countryName.length()){
                //compare the String in EditText with Names in the ArrayList
                  if(searchString.equalsIgnoreCase(countryName.substring(0,textLength)))
                  searchResults.add(originalValues.get(i));
                }
                 }
  
                 adapter.notifyDataSetChanged();
               }
  public void beforeTextChanged(CharSequence s, int start, int count,
       int after) {
  
  
     }
  
     public void afterTextChanged(Editable s) {
  
  
     }
    });
  </code></pre>
  
  <p>MainActvity.java</p>
  
  <pre><code>Public class MainActivity extends Activity {
   EditText searchBox;
  ArrayList&lt;HashMap&lt;String, Object&gt;&gt; searchResults;
  ArrayList&lt;HashMap&lt;String, Object&gt;&gt; originalValues;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
     searchBox = (EditText)findViewById(R.id.searchBox);
  
          /** This is the XML data to be parsed */
          String xmlData = "&lt;countries&gt;" +
                              "&lt;country name='India' flag='"+Integer.toString(R.drawable.india) +"'&gt;" +
                                  "&lt;language&gt;Hindi&lt;/language&gt;" +
                                  "&lt;capital city='New Delhi' /&gt;" +
                                  "&lt;currency code='INR'&gt;Indian Rupee&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Pakistan' flag='"+Integer.toString(R.drawable.pakistan) +"'&gt;" +
                                  "&lt;language&gt;Urdu&lt;/language&gt;" +
                                  "&lt;capital city='Islamabad' /&gt;" +
                                  "&lt;currency code='PKR'&gt;Pakistani Rupee&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Sri Lanka' flag='"+Integer.toString(R.drawable.srilanka) +"'&gt;" +
                                  "&lt;language&gt;Sinhala&lt;/language&gt;" +
                                  "&lt;capital city='Sri Jayawardenapura Kotte' /&gt;" +
                                  "&lt;currency code='LKR'&gt;Sri Lankan Rupee&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='China' flag='"+Integer.toString(R.drawable.china) +"'&gt;" +
                                  "&lt;language&gt;Chineese&lt;/language&gt;" +
                                  "&lt;capital city='Beijing' /&gt;" +
                                  "&lt;currency code='CNY'&gt;Renminbi&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Bangladesh' flag='"+Integer.toString(R.drawable.bangladesh) +"'&gt;" +
                                  "&lt;language&gt;Bangla&lt;/language&gt;" +
                                  "&lt;capital city='Dhaka' /&gt;" +
                                  "&lt;currency code='BDT'&gt;Taka&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Nepal' flag='"+Integer.toString(R.drawable.nepal) +"'&gt;" +
                                  "&lt;language&gt;Nepal Bhasa&lt;/language&gt;" +
                                  "&lt;capital city='Kathmandu' /&gt;" +
                                  "&lt;currency code='NPR'&gt;Nepalese rupee&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Afghanistan' flag='"+Integer.toString(R.drawable.afghanistan) +"'&gt;" +
                                  "&lt;language&gt;Dari Persian&lt;/language&gt;" +
                                  "&lt;capital city='Kabul' /&gt;" +
                                  "&lt;currency code='AFN'&gt;Afghani&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='North Korea' flag='"+Integer.toString(R.drawable.nkorea) +"'&gt;" +
                                  "&lt;language&gt;Korean&lt;/language&gt;" +
                                  "&lt;capital city='Pyongyang' /&gt;" +
                                  "&lt;currency code='KPW'&gt;North Korean won&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='South Korea' flag='"+Integer.toString(R.drawable.skorea) +"'&gt;" +
                                  "&lt;language&gt;Korean&lt;/language&gt;" +
                                  "&lt;capital city='Seoul' /&gt;" +
                                  "&lt;currency code='KRW'&gt;South Korean won&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
  
                              "&lt;country name='Japan' flag='"+Integer.toString(R.drawable.japan) +"'&gt;" +
                                  "&lt;language&gt;Japanese&lt;/language&gt;" +
                                  "&lt;capital city='Tokyo' /&gt;" +
                                  "&lt;currency code='JPY'&gt;Yen&lt;/currency&gt;" +
                              "&lt;/country&gt;" +
                          "&lt;/countries&gt;";
  
          /** The parsing of the xml data is done in a non-ui thread */
          ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
  
          /** Start parsing xml data */
          listViewLoaderTask.execute(xmlData);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
      private class ListViewLoaderTask extends AsyncTask&lt;String, Void, SimpleAdapter&gt;{
  
          /** Doing the parsing of xml data in a non-ui thread */
          @Override
          protected SimpleAdapter doInBackground(String... xmlData) {
              StringReader reader = new StringReader(xmlData[0]);
  
              CountryXmlParser countryXmlParser = new CountryXmlParser();
  
              List&lt;HashMap&lt;String, String&gt;&gt; countries = null;
  
              try{
                  /** Getting the parsed data as a List construct */
                  countries = countryXmlParser.parse(reader);
              }catch(Exception e){
                  Log.d("Exception",e.toString());
              }
  
              /** Keys used in Hashmap */
              String[] from = { "country","flag","details"};
  
              /** Ids of views in listview_layout */
              int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details};
  
              /** Instantiating an adapter to store each items
              *  R.layout.listview_layout defines the layout of each item
              */
              SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);
  
              return adapter;
          }
  
          @Override
         protected void onPostExecute(SimpleAdapter adapter) {
  
          /** Getting a reference to listview of main.xml layout file */
          ListView listView = ( ListView ) findViewById(R.id.lv_countries);
          adapter = adapter;
          originalValues=new ArrayList&lt;HashMap&lt;String,Object&gt;&gt;();
          originalValues.add(hm);
          searchResults=new ArrayList&lt;HashMap&lt;String,Object&gt;&gt;(originalValues);
          searchBox.addTextChangedListener(new TextWatcher() {
  
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //get the text in the EditText
                    String searchString=searchBox.getText().toString();
                    int textLength=searchString.length();
  
                           //clear the initial data &lt;span class="h9umc81m835" id="h9umc81m835_8"&gt;set&lt;/span&gt;
                           searchResults.clear();
  
                    for(int i=0;i&lt;originalValues.size();i++)
                    {
                   String countryName=originalValues.get(i).get("country").toString();
                   if(textLength&lt;=countryName.length()){
                   //compare the String in EditText with Names in the ArrayList
                     if(searchString.equalsIgnoreCase(countryName.substring(0,textLength)))
                     searchResults.add(originalValues.get(i));
                   }
                    }
  
                    adapter.notifyDataSetChanged();
                  }
     public void beforeTextChanged(CharSequence s, int start, int count,
          int after) {
  
  
        }
  
        public void afterTextChanged(Editable s) {
  
  
        }
       });
          /** Setting the adapter containing the country list to listview */
          listView.setAdapter(adapter);
      }       
  }
  </code></pre>
  
  <p>}</p>
  
  <p>CountryXmlParser.java</p>
  
  <pre><code>public class CountryXmlParser { {
  
      private static final String ns = null;
  
      /** This is the only function need to be called from outside the class */
      public List&lt;HashMap&lt;String, String&gt;&gt; parse(Reader reader)
              throws XmlPullParserException, IOException{
          try{
              XmlPullParser parser = Xml.newPullParser();
              parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
              parser.setInput(reader);
              parser.nextTag();
              return readCountries(parser);
          }finally{
          }
      }
  
      /** This method read each country in the xml data and add it to List */
      private List&lt;HashMap&lt;String, String&gt;&gt; readCountries(XmlPullParser parser)
              throws XmlPullParserException,IOException{
  
          List&lt;HashMap&lt;String, String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();
  
          parser.require(XmlPullParser.START_TAG, ns, "countries");
  
          while(parser.next() != XmlPullParser.END_TAG){
              if(parser.getEventType() != XmlPullParser.START_TAG){
                  continue;
              }
  
              String name = parser.getName();
              if(name.equals("country")){
                  list.add(readCountry(parser));
              }
              else{
                  skip(parser);
              }
          }
          return list;
      }
  
      /** This method read a country and returns its corresponding HashMap construct */
      private HashMap&lt;String, String&gt; readCountry(XmlPullParser parser)
              throws XmlPullParserException, IOException{
  
          parser.require(XmlPullParser.START_TAG, ns, "country");
  
          String countryName = parser.getAttributeValue(ns, "name");
          String flag = parser.getAttributeValue(ns, "flag");
          String language="";
          String capital="";
          String currencyCode="";
          String currency="";
  
          while(parser.next() != XmlPullParser.END_TAG){
              if(parser.getEventType() != XmlPullParser.START_TAG){
                  continue;
              }
  
              String name = parser.getName();
  
              if(name.equals("language")){
                  language = readLanguage(parser);
              }else if(name.equals("capital")){
                  capital = parser.getAttributeValue(ns, "city");
                  readCapital(parser);
              }else if(name.equals("currency")){
                  currencyCode = parser.getAttributeValue(ns, "code");
                  currency = readCurrency(parser);
              }else{
                  skip(parser);
              }
          }
  
          String details =    "Language : " + language + "\n" +
                              "Capital : " + capital + "\n" +
                              "Currency : " + currency + "(" + currencyCode + ")";
  
          HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;();
          hm.put("country", countryName);
          hm.put("flag", flag);
          hm.put("details",details);
  
          return hm;
      }
  
      /** Process language tag in the xml data */
      private String readLanguage(XmlPullParser parser)
              throws IOException, XmlPullParserException {
          parser.require(XmlPullParser.START_TAG, ns, "language");
          String language = readText(parser);
          return language;
      }
  
      /** Process Capital tag in the xml data */
      private void readCapital(XmlPullParser parser)
              throws IOException, XmlPullParserException {
          parser.require(XmlPullParser.START_TAG, ns, "capital");
          parser.nextTag();
      }
  
      /** Process Currency tag in the xml data */
      private String readCurrency(XmlPullParser parser)
              throws IOException, XmlPullParserException {
          parser.require(XmlPullParser.START_TAG, ns, "currency");
          String currency = readText(parser);
          return currency;
      }
  
      /** Getting Text from an element */
      private String readText(XmlPullParser parser)
              throws IOException, XmlPullParserException{
          String result = "";
          if(parser.next()==XmlPullParser.TEXT){
              result = parser.getText();
              parser.nextTag();
          }
          return result;
      }
  
      private void skip(XmlPullParser parser)
              throws XmlPullParserException, IOException {
          if (parser.getEventType() != XmlPullParser.START_TAG) {
             throw new IllegalStateException();
          }
          int depth = 1;
          while (depth != 0) {
              switch (parser.next()) {
                  case XmlPullParser.END_TAG:
                      depth--;
                      break;
                  case XmlPullParser.START_TAG:
                      depth++;
                      break;
              }
          }
      }
  }
  </code></pre>