title: How to apply search filter for first and last name
body: <p>I want to apply search filter like Android mobile have in contact app. For example there is a contact named <strong>Aakash sharma</strong>. And in search bar I press starting latter of <strong>Aakash</strong> i.e <strong>aa</strong> and after space then I press last name i.e. <strong>sh</strong>. Please find attachment so that you can understand batter.<img src="https://i.stack.imgur.com/nruBP.jpg" alt="enter image description here"> Please help me. Thank you very much in advance.</p>
  
  <p>Here is my code</p>
  
  <pre><code>EditText search_filter = (EditText) stn_dialog
                      .findViewById(R.id.et_search_ds);
              ListView listView = (ListView) stn_dialog
                      .findViewById(R.id.lv_search_ds);
              stn_adapter = new ListAdapterStation(
                      RetailerSelectionActivity.this, R.layout.station_list,
                      station_list);
  
              filterTextWatcher = new TextWatcher() {
                  public void afterTextChanged(Editable s) {
                      // Filter from my adapter
                      stn_adapter.getFilter().filter(s);
                      // Update my view
                      stn_adapter.notifyDataSetChanged();
                  }
  
                  public void beforeTextChanged(CharSequence s, int start,
                          int count, int after) {
                  }
  
                  public void onTextChanged(CharSequence s, int start,
                          int before, int count) {
                  }
              };
              search_filter.addTextChangedListener(filterTextWatcher);
              listView.setAdapter(stn_adapter);
  
              listView.setOnItemClickListener(new OnItemClickListener() {
  
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                          int pos, long id) {
                      stn_dialog.dismiss();
                      RetailerSelectionDTO retailerSelectionDTO = (RetailerSelectionDTO) station_list
                              .get(pos);
                      station_name = retailerSelectionDTO.getStation_name_c();
                      station_id = retailerSelectionDTO.getStation_id_c();
                      btn_station.setText(station_name);
                      btn_retailer.requestFocus();
                      new GetRetailerTask().execute();
                  }
              });
  </code></pre>
  
  <p>And the Adapter is</p>
  
  <pre><code>private class ListAdapterStation extends ArrayAdapter {
      /**
       * List of Procedure
       * 
       */
      private ArrayList&lt;RetailerSelectionDTO&gt; items;
      /**
       * Application Context
       */
      private Context mContext;
      /**
       * List of Procedure
       */
      private ArrayList&lt;RetailerSelectionDTO&gt; originalItems = new ArrayList&lt;RetailerSelectionDTO&gt;();
  
      private ListAdapterItemFilter filter;
  
      public ListAdapterStation(Context context, int textViewResourceId,
              List&lt;RetailerSelectionDTO&gt; list) {
          super(context, textViewResourceId, list);
          this.items = (ArrayList&lt;RetailerSelectionDTO&gt;) list;
          this.mContext = context;
          cloneItems((ArrayList&lt;RetailerSelectionDTO&gt;) list);
      }
  
      /**
       * cloneItems @
       * 
       * @param list
       */
      protected void cloneItems(ArrayList&lt;RetailerSelectionDTO&gt; list) {
          for (Iterator&lt;RetailerSelectionDTO&gt; iterator = list.iterator(); iterator
                  .hasNext();) {
              RetailerSelectionDTO p = iterator.next();
              originalItems.add(p);
          }
      }
  
      /**
       * Get Count
       */
  
      @Override
      public int getCount() {
          return items != null ? items.size() : 0;
      }
  
      /**
       * Get Item
       * 
       * @param item
       * @return TranUserProcedureDTO
       */
      @Override
      public RetailerSelectionDTO getItem(int item) {
          RetailerSelectionDTO p = null;
          p = (items != null ? items.get(item) : null);
          return p;
  
      }
  
      /**
       * Get View
       * 
       * @param position
       * @param convertView
       * @param parent
       * @return View
       */
      public View getView(int position, View convertView, ViewGroup parent) {
          View view = convertView;
          try {
              if (convertView == null) {
                  LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  view = vi.inflate(R.layout.station_list, null); // --CloneChangeRequired(list_item)
              } else {
                  view = convertView;
              }
              RetailerSelectionDTO retailerSelectionDTO = items.get(position);
              if (retailerSelectionDTO != null) {
                  TextView textView = (TextView) view
                          .findViewById(R.id.tv_station);
                  textView.setText(retailerSelectionDTO.getStation_name_c());
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return view;
      }
  
      public Filter getFilter() {
          if (filter == null) {
              filter = new ListAdapterItemFilter();
          }
          return filter;
      }
  
      private class ListAdapterItemFilter extends Filter {
  
          protected FilterResults performFiltering(CharSequence prefix) {
              /**
               * Initiate our results object
               */
              FilterResults results = new FilterResults();
  
              /**
               * No prefix is sent to filter by so we're going to send back
               * the original array
               */
  
              if (prefix == null || prefix.length() == 0) {
                  results.values = originalItems;
                  results.count = originalItems.size();
              } else {
                  /**
                   * Compare lower case strings
                   */
                  String prefixString = prefix.toString().toLowerCase();
                  final ArrayList&lt;RetailerSelectionDTO&gt; filteredItems = new ArrayList&lt;RetailerSelectionDTO&gt;();
                  final ArrayList&lt;RetailerSelectionDTO&gt; localItems = new ArrayList&lt;RetailerSelectionDTO&gt;();
                  localItems.addAll(originalItems);
                  final int count = localItems.size();
                  for (int i = 0; i &lt; count; i++) {
                      final RetailerSelectionDTO item = localItems.get(i);
                      final String itemName = item.getStation_name_c()
                              .toLowerCase();
                      if (itemName.contains(prefixString)) {
                          filteredItems.add(item);
                      } else {
                      }
                  }
                  results.values = filteredItems;
                  results.count = filteredItems.size();
              }
              return results;
          }
  
          @SuppressWarnings("unchecked")
          @Override
          /**
           * 
           */
          protected void publishResults(CharSequence prefix,
                  FilterResults results) {
              /**
               * noinspection unchecked
               */
              final ArrayList&lt;RetailerSelectionDTO&gt; localItems = (ArrayList&lt;RetailerSelectionDTO&gt;) results.values;
              notifyDataSetChanged();
              clear();
              /**
               * Add the items back in
               */
              for (Iterator iterator = localItems.iterator(); iterator
                      .hasNext();) {
                  RetailerSelectionDTO gi = (RetailerSelectionDTO) iterator
                          .next();
                  add(gi);
              }
          }
      }
  }
  </code></pre>