title: How to add search function code used for "search bar" inside Onclick of "search button" in android
body: <p>I have 2 activities, in 1st activity there is an "keyword" and "search" button, based on the keyword given related items should be displayed in the second activity in listview form, but just to test i hv placed search bar on top of the listview and coded for it, and this works fine, now the issue s i want to move "search function code" in 2nd activity inside onclick of "search" button. </p>
  
  <p>here's my code</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  
      StrictMode.setThreadPolicy(policy); 
  
      final EditText searchBox=(EditText) findViewById(R.id.search);
      final ListView  list=(ListView)findViewById(android.R.id.list);
  
      //get the LayoutInflater for inflating the customomView
       //this will be used in the custom adapter
       inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  
    final  ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          // Creating JSON Parser instance
                  final JSONParser jParser = new JSONParser();
  
                  // getting JSON string from URL
                  JSONObject json = jParser.getJSONFromUrl(URL);
                  try {
                       posts = json.getJSONArray(KEY_POSTS);
  
          // looping through all song nodes &lt;song&gt;
                  for(int i = 0; i &lt; posts.length(); i++){
                      JSONObject c = posts.getJSONObject(i);
                      // Storing each json item in variable
                      String id = c.getString(KEY_ID);
                      String title = c.getString(KEY_TITLE);
                      String date = c.getString(KEY_DATE);
                      String content = c.getString(KEY_CONTENT);
                      // to remove all &lt;P&gt; &lt;/p&gt; and &lt;br /&gt; and replace with ""
                       content = content.replace("&lt;br /&gt;", "");
                       content = content.replace("&lt;p&gt;", "");
                       content = content.replace("&lt;/p&gt;", "");
  
                      //authornumber is agin  JSON Object
                      JSONObject author = c.getJSONObject(KEY_AUTHOR);
                      String name = author.getString(KEY_NAME);
  
                      String url = null;
                      String slug = null;
                      try {
                      JSONArray atta = c.getJSONArray("attachments");
                      for(int j = 0; j &lt; atta.length(); j++){
                          JSONObject d = atta.getJSONObject(j);
  
                          slug = d.getString(KEY_SLUG);
  
                          JSONObject images = d.getJSONObject(KEY_IMAGES);
  
                          JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                          url = thumbnail.getString(KEY_URL);
  
                      }
                      } catch (Exception e) {
                          e.printStackTrace();
  
                      }
  
              // creating new HashMap
              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
              // adding each child node to HashMap key =&gt; value
              map.put(KEY_ID, id);
              map.put(KEY_TITLE, title);
              map.put(KEY_DATE, date);
              map.put(KEY_NAME, name);
              map.put(KEY_CONTENT, content);
              map.put(KEY_SLUG, slug);
              map.put(KEY_URL, url);
    //                System.out.println("the map is title "+map.get("title"));
           //title2.add(map.get("title"));
              // adding HashList to ArrayList
              songsList.add(map);
                  }   
  
                  }catch (JSONException e) {
                      e.printStackTrace();
  
                      }
  
  
                //searchResults=OriginalValues initially
                  searchResults=new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(songsList);
  
           // Getting adapter by passing json data ArrayList
              adapter=new LazyAdapter(this, songsList);   
  
               list.setAdapter(adapter);
  
  
               searchBox.addTextChangedListener(new TextWatcher() {
  
                   public void onTextChanged(CharSequence s, int start, int before, int count) {
                     //get the text in the EditText
                      searchString=searchBox.getText().toString();
                      textLength=searchString.length();
                     searchResults.clear();
  
                     for(int i=0;i&lt;songsList.size();i++)
                     {
                     playerName=songsList.get(i).get("title").toString();
                    System.out.println("player name "+playerName);
                    if(textLength&lt;=playerName.length()){
                    //compare the String in EditText with Names in the    ArrayList
                           if(searchString.equalsIgnoreCase(playerName.substring(0,textLength))){
                      searchResults.add(songsList.get(i));
                      System.out.println("the array list is "+songsList.get(i));
                      adapter=new LazyAdapter(Home.this, searchResults);   
  
                       list.setAdapter(adapter);
                      }
  
                        }
  </code></pre>