Note that there are some explanatory texts on larger screens.

plurals
  1. POpostition of ticked (checked) checkbox changes after filtering data of Listview
    text
    copied!<p>I am trying to apply filter on my custom adapter which extends <code>BaseAdapter</code>, in which I am facing some problems, after I filter input based on the text in <code>EditText</code> and check the <code>CheckBox</code> to select one value and if I erase the text in the <code>EditText</code> to search for some other thing the position of the checked checkbox changes automatically as you can see in the image attached below. I am currently facing two problems</p> <p>1 - Position of the ticked checkbox changes automatically</p> <p>2 - If I type in uppercase in the <code>EditText</code> the <code>ListView</code> goes blank.</p> <pre><code>public class MyCustomAdapter extends BaseAdapter implements Filterable { Context mContext; private LayoutInflater mInflater; SparseBooleanArray mSparseBooleanArray; private ArrayList&lt;Map&lt;String,String&gt;&gt; mAdapData = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); private ArrayList&lt;Map&lt;String,String&gt;&gt; mOriginalData = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); public MyCustomAdapter(Context mContext) { mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mSparseBooleanArray = new SparseBooleanArray(); } public ArrayList&lt;String&gt; getCheckedItems() { ArrayList&lt;String&gt; mTempArry = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; mAdapData.size(); i++) { if (mSparseBooleanArray.get(i)) { Map&lt;String, String&gt; map = (Map&lt;String, String&gt;) mAdapData.get(i); final String numbr = map.get("Phone").toString(); mTempArry.add(numbr); } } return mTempArry; } @Override public int getCount() { return this.mAdapData.size(); } public void addItem(String paramString1, String paramString2) { Map&lt;String, String&gt; NameNumber = new HashMap&lt;String, String&gt;(); NameNumber.put("Name", paramString1); NameNumber.put("Phone", paramString2); this.mAdapData.add(NameNumber); this.mOriginalData.add(NameNumber); notifyDataSetChanged(); } @SuppressWarnings("unchecked") public Object getItem(int paramInt) { return (ArrayList&lt;Map&lt;String, String&gt;&gt;) this.mAdapData.get(paramInt); } @Override public long getItemId(int paramInt) { return paramInt; } @Override public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) { ViewHolder viewHolder; if (paramView == null) { viewHolder = new ViewHolder(); paramView = mInflater.inflate(R.layout.multiplecontactview, null); viewHolder.tvName = (TextView) paramView.findViewById(R.id.txtContactName); viewHolder.tvNumber = (TextView) paramView.findViewById(R.id.txtContactNumber); viewHolder.cb = (CheckBox) paramView.findViewById(R.id.checkBox1); viewHolder.cb.setTag(paramInt); viewHolder.cb.setChecked(mSparseBooleanArray.get(paramInt)); viewHolder.cb.setOnCheckedChangeListener(mCheckedChangeListener); viewHolder.tvName.setTextColor(Color.BLACK); viewHolder.tvNumber.setTextColor(Color.BLACK); for (int i = 0; i &lt; mAdapData.size(); i++) { Map&lt;String, String&gt; map = (Map&lt;String, String&gt;) mAdapData.get(paramInt); final String name = map.get("Name").toString(); final String numbr = map.get("Phone").toString(); viewHolder.tvName.setText(name); viewHolder.tvNumber.setText(numbr); } paramView.setTag(viewHolder); } else { viewHolder = (ViewHolder) paramView.getTag(); } return paramView; } OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked); } }; public static class ViewHolder { TextView tvName; TextView tvNumber; CheckBox cb; } @Override public Filter getFilter() { return new MyContactFilter(); } @SuppressLint("DefaultLocale") private class MyContactFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); ArrayList&lt;Map&lt;String, String&gt;&gt; mFilteredData = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); if (!TextUtils.isEmpty(constraint)) { for(int i = 0; i &lt; mAdapData.size(); i++) { Map&lt;String, String&gt; map = (Map&lt;String, String&gt;) mAdapData.get(i); final String names = map.get("Name").toString(); final String numbr = map.get("Phone").toString(); if(names.toLowerCase().contains(constraint.toString().toLowerCase())) { Map&lt;String, String&gt; FilNameNumber = new HashMap&lt;String, String&gt;(); FilNameNumber.put("Name", names); FilNameNumber.put("Phone", numbr); mFilteredData.add(FilNameNumber); } } results.values = mFilteredData; results.count = mFilteredData.size(); } else { synchronized (mOriginalData) { results.values = mOriginalData; results.count = mOriginalData.size(); } } return results; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence cs, FilterResults fr) { mAdapData = (ArrayList&lt;Map&lt;String, String&gt;&gt;) fr.values; notifyDataSetChanged(); } } } </code></pre> <p><img src="https://i.stack.imgur.com/rTaxO.png" alt="Screenshot"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload