title: Android : Change CheckBox checked value in ListView when getFilter() called
body: <p>I recently <a href="https://stackoverflow.com/q/30724689/1318946">posted</a> my Question for <code>MultiSpinner</code> values to choose Multiple values from Spinner dialog. I've done successfully with following class.</p>
  
  <p><strong>MultiSpinnerSearch.java</strong></p>
  
  <pre><code>package com.example.multiplechoicelistwithfilter;
  
  import java.util.List;
  
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnCancelListener;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.util.AttributeSet;
  import android.util.SparseBooleanArray;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.Spinner;
  
  public class MultiSpinnerSearch extends Spinner implements OnCancelListener {
  
      private List&lt;Hello&gt; items;
      //private boolean[] selected;
      private String defaultText;
      private MultiSpinnerListener listener;
  
      public MultiSpinnerSearch(Context context) {
          super(context);
      }
  
      public MultiSpinnerSearch(Context arg0, AttributeSet arg1) {
          super(arg0, arg1);
      }
  
      public MultiSpinnerSearch(Context arg0, AttributeSet arg1, int arg2) {
          super(arg0, arg1, arg2);
      }
  
      @Override
      public void onCancel(DialogInterface dialog) {
          // refresh text on spinner
  
          StringBuffer spinnerBuffer = new StringBuffer();
  
          for (int i = 0; i &lt; items.size(); i++) {
              if (items.get(i).isSelected() == true) {
                  spinnerBuffer.append(items.get(i).getName());
                  spinnerBuffer.append(", ");
              }
          }
  
          String spinnerText = "";
          spinnerText = spinnerBuffer.toString();
          if (spinnerText.length() &gt; 2)
              spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
          else
              spinnerText = defaultText;
  
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getContext(),
                  android.R.layout.simple_dropdown_item_1line,
                  new String[] { spinnerText });
          setAdapter(adapter);
  
          listener.onItemsSelected(items);
      }
  
  
      @Override
      public boolean performClick() {
  
          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
          builder.setTitle(defaultText);
  
          LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
  
          View view = inflater.inflate(R.layout.alert_dialog_listview_search, null);
          builder.setView(view);
  
          final ListView listView = (ListView) view.findViewById(R.id.alertSearchListView);
          listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
          final MyAdapter adapter = new MyAdapter(getContext(), items);
          listView.setAdapter(adapter);
  
          EditText editText = (EditText) view.findViewById(R.id.alertSearchEditText);
          editText.addTextChangedListener(new TextWatcher() {
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
  
                  adapter.getFilter().filter(s.toString());
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }
  
              @Override
              public void afterTextChanged(Editable s) {  
              }
          });
  
          //builder.setMultiChoiceItems(items.toArray(new CharSequence[items.size()]), selected, this);
          builder.setPositiveButton(android.R.string.ok,
                  new DialogInterface.OnClickListener() {
  
              @Override
              public void onClick(DialogInterface dialog, int which) {
  
                  SparseBooleanArray sp = listView.getCheckedItemPositions();
  
                  for(int i=0;i&lt;items.size();i++) {
                      items.get(i).setSelected(sp.get(i));
                  }               
                  dialog.cancel();
              }
          });
  
          builder.setOnCancelListener(this);
          builder.show();
          return true;
      }
  
      public void setItems(List&lt;Hello&gt; items, String allText, int position,
              MultiSpinnerListener listener) {
  
          this.items = items;
          this.defaultText = allText;
          this.listener = listener;
  
          // all selected by default
          //selected = new boolean[items.size()];
          //for (int i = 0; i &lt; selected.length; i++)
          //  selected[i] = false;    
  
          if(position != -1)
          {
              items.get(position).setSelected(true);
              listener.onItemsSelected(items);
  
              onCancel(null);
          }
  
      }
  
      public interface MultiSpinnerListener {
          public void onItemsSelected(List&lt;Hello&gt; items);
      } 
  }
  </code></pre>
  
  <p><strong>MyAdapter class:</strong></p>
  
  <pre><code>package com.example.multiplechoicelistwithfilter;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import android.annotation.SuppressLint;
  import android.content.Context;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.CheckedTextView;
  import android.widget.Filter;
  import android.widget.Filterable;
  
  public class MyAdapter extends BaseAdapter implements Filterable {
  
          List&lt;Hello&gt; arrayList;      
          List&lt;Hello&gt; mOriginalValues; // Original Values
          LayoutInflater inflater;
  
          public MyAdapter(Context context, List&lt;Hello&gt; arrayList) {
              this.arrayList = arrayList;
              inflater = LayoutInflater.from(context);
          }
  
          @Override
          public int getCount() {
              return arrayList.size();
          }
  
          @Override
          public Object getItem(int position) {
              return position;
          }
  
          @Override
          public long getItemId(int position) {
              return position;
          }
  
          private class ViewHolder {
              CheckedTextView textView;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
  
              ViewHolder holder = null;
  
              if (convertView == null) {
  
                  holder = new ViewHolder();
                  convertView = inflater.inflate(android.R.layout.select_dialog_multichoice, null);
                  holder.textView = (CheckedTextView) convertView;
  
                  convertView.setTag(holder);
              } else {
                  holder = (ViewHolder) convertView.getTag();
              }
              holder.textView.setText(arrayList.get(position).getName());
              holder.textView.setChecked(arrayList.get(position).isSelected());
              return convertView;
          }
  
          @SuppressLint("DefaultLocale")
          @Override
          public Filter getFilter() {
              Filter filter = new Filter() {
  
                  @SuppressWarnings("unchecked")
                  @Override
                  protected void publishResults(CharSequence constraint,FilterResults results) {
  
                      arrayList = (List&lt;Hello&gt;) results.values; // has the filtered values
                      notifyDataSetChanged();  // notifies the data with new filtered values
                  }
  
                  @Override
                  protected FilterResults performFiltering(CharSequence constraint) {
                      FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                      List&lt;Hello&gt; FilteredArrList = new ArrayList&lt;Hello&gt;();
  
                      if (mOriginalValues == null) {
                          mOriginalValues = new ArrayList&lt;Hello&gt;(arrayList); // saves the original data in mOriginalValues
                      }
  
                      /********
                       * 
                       *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                       *  else does the Filtering and returns FilteredArrList(Filtered)  
                       *
                       ********/
                      if (constraint == null || constraint.length() == 0) {
  
                          // set the Original result to return  
                          results.count = mOriginalValues.size();
                          results.values = mOriginalValues;
                      } else {
                          constraint = constraint.toString().toLowerCase();
                          for (int i = 0; i &lt; mOriginalValues.size(); i++) {
                              Log.i("TAG", "" + mOriginalValues.get(i).getName() + " -&gt; " + mOriginalValues.get(i).isSelected());
                              String data = mOriginalValues.get(i).getName();
                              if (data.toLowerCase().contains(constraint.toString())) {
                                  FilteredArrList.add(mOriginalValues.get(i));
                              }
                          }
                          // set the Filtered result to return
                          results.count = FilteredArrList.size();
                          results.values = FilteredArrList;
                      }
                      return results;
                  }
              };
              return filter;
          }
      }
  </code></pre>
  
  <blockquote>
    <p>I am getting output successfully as below, but in first image suppose
    i am select first two image and then filtering word 'ball' it gives me
    filtered result with <strong>selected checkbox as before.</strong></p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/OefUm.png" alt="enter image description here"></p>
  
  <blockquote>
    <p>You can see that first two items not selected before but it shows
    selected while filtering. </p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/C33IF.png" alt="enter image description here"></p>
  
  <p>I am stuck with this silly problem since last couple of hours. Help me.</p>
  
  <p>Your help would be appreciated. Thanks.</p>