title: handle list view with check box and search implemention
body: <p>i have a list view which has check box on each row and i`ve implemented a serch view to search in the list view. the problem is that when i search the list view and select item at postion X from the modifided list, delete the text from the search view i see the original list with the item checked in position X.</p>
  
  <p>Example - list of a,b,c....z .</p>
  
  <p>i can see 5 items on the list:</p>
  
  <p>A
  B
  C
  D
  E</p>
  
  <p>then search for z so i see only Z and check it. then i delete Z and see A-E again and A is checked.</p>
  
  <p>this is my main:</p>
  
  <pre><code>public class AddRoomatesScreen extends Activity implements AdapterView.OnItemClickListener,SearchView.OnQueryTextListener {
  SharedPreferences preferences;
  List&lt;String&gt; nameList = new ArrayList&lt;String&gt;();
  List&lt;String&gt; phoneList = new ArrayList&lt;String&gt;();
  MyAdapter adapter;
  Button btnSelect;
  String apartmentnumber;
  SearchView searchView;
  ArrayList&lt;Contact&gt; contactList = new ArrayList&lt;&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.add_roomate);
      preferences = getSharedPreferences("appData", 0);
      int apartmentNumber = preferences.getInt("apartmentNumber", 0);
      apartmentnumber = Integer.toString(apartmentNumber);
      getAllContacts(this.getContentResolver());
      ListView lv = (ListView) findViewById(R.id.lv);
      adapter = new MyAdapter(nameList,contactList);
      lv.setAdapter(adapter);
      lv.setOnItemClickListener(this);
      lv.setItemsCanFocus(false);
      lv.setTextFilterEnabled(true);
      searchView = (SearchView) findViewById(R.id.search_view);
      searchView.setOnQueryTextListener(this);
      // adding
      btnSelect = (Button) findViewById(R.id.addSelectedContacts);
      btnSelect.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              for(int i = 0; i&lt;adapter.mCheckStates.size(); i++){
                  Contact contact = (adapter.contactsListCopy.get(adapter.mCheckStates.keyAt(i)));
                  new addRoommate().execute(apartmentnumber, contact.getPhoneNumber());
              }
              preferences = getSharedPreferences("appData", 0);
              SharedPreferences.Editor editor = preferences.edit();
              editor.putBoolean("roomatesLoadedFromDB", false);
              editor.apply();
          }
      });
  
  
  }
  
  @Override
  public boolean onQueryTextSubmit(String query) {
      return false;
  }
  
  @Override
  public boolean onQueryTextChange(String newText) {
      adapter.getFilter().filter(newText);
      return false;
  }
  
  @Override
  public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
      // TODO Auto-generated method stub
      adapter.toggle(arg2);
  }
  
  public void getAllContacts(ContentResolver cr) {
  
      Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
      while (phones.moveToNext()) {
          String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          nameList.add(name);
          phoneList.add(phoneNumber);
          contactList.add(new Contact(name,phoneNumber));
      }
  
      phones.close();
  }
  </code></pre>
  
  <p>this is my adapter:</p>
  
  <pre><code>class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener,Filterable {
      private SparseBooleanArray mCheckStates;
      LayoutInflater mInflater;
      TextView  tv;
      CheckBox cb;
      ValueFilter filter;
      ArrayList&lt;Contact&gt; contactsList;
      ArrayList&lt;Contact&gt; contactsListCopy;
  
      MyAdapter(List&lt;String&gt; nameList , ArrayList&lt;Contact&gt; contactsList) {
          mCheckStates = new SparseBooleanArray(contactsList.size());
          mInflater = (LayoutInflater) AddRoomatesScreen.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          this.contactsList = contactsList;
          this.contactsListCopy = this.contactsList;
          getFilter();
      }
  
      @Override
      public int getCount() {
          return contactsListCopy.size();
      }
  
      @Override
      public Object getItem(int position) {
          return contactsListCopy.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
          View vi = convertView;
          if (convertView == null)
              vi = mInflater.inflate(R.layout.contact_list_item, null);
          TextView tv = (TextView) vi.findViewById(R.id.textView3);
  
          cb = (CheckBox) vi.findViewById(R.id.checkBox);
          Contact contact = contactsListCopy.get(position);
          tv.setText(contact.getName());
          cb.setTag(position);
          cb.setChecked(mCheckStates.get(position, false));
          cb.setOnCheckedChangeListener(this);
  
          return vi;
      }
  
      public boolean isChecked(int position) {
          return mCheckStates.get(position, false);
      }
  
      public void setChecked(int position, boolean isChecked) {
          mCheckStates.put(position, isChecked);
          notifyDataSetChanged();
      }
  
      public void toggle(int position) {
          setChecked(position, !isChecked(position));
      }
  
      @Override
      public void onCheckedChanged(CompoundButton buttonView,
                                   boolean isChecked) {
  
          mCheckStates.put((Integer) buttonView.getTag(), isChecked);
      }
  
      @Override
      public Filter getFilter() {
          if (filter == null){
              filter = new ValueFilter();
          }
              return filter;
  
      }
  
          private class ValueFilter extends Filter {
  
  
              @Override
              protected FilterResults performFiltering(CharSequence constraint) {
                  FilterResults results = new FilterResults();
  
                  if (constraint != null &amp;&amp; constraint.length() &gt; 0) {
                      ArrayList&lt;Contact&gt; temp = new ArrayList&lt;Contact&gt;();
                      for (int i = 0; i &lt; contactList.size(); i++) {
                          Contact c = contactList.get(i);
                          if ((c.getName().toString())
                                  .contains(constraint.toString())) {
                              temp.add(c);
                          }
                      }
                      results.count = temp.size();
                      results.values = temp;
                  } else {
                      results.count = contactList.size();
                      results.values = contactList;
                  }
                  return results;
  
              }
  
              @Override
              protected void publishResults(CharSequence constraint, FilterResults results) {
                  contactsListCopy = (ArrayList&lt;Contact&gt;) results.values;
                  notifyDataSetChanged();
  
              }
          }
  
  
  }
  </code></pre>
  
  <p>this is my model:</p>
  
  <pre><code>public class Contact{
      private String phoneNumber;
      private String name;
  
      public Contact(String name , String phoneNumber){
          this.phoneNumber = phoneNumber;
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
      public String getPhoneNumber() {
          return phoneNumber;
      }
  
  }
  </code></pre>