Filtering custom adapter returning incorrect results

前端 未结 3 1352
盖世英雄少女心
盖世英雄少女心 2021-01-28 23:19

Check:

\"screenshot\"

I am trying to implement filter on my ListView. But I am facing a very s

3条回答
  •  礼貌的吻别
    2021-01-28 23:49

    Your problems come from the way you setup the getView() method and how you set the checked item in that SparseBooleanArray. I've edited your adapter's code and it should work now. The main changes are in the OnCheckedChangeListener field:

    public class MyCustomAdapter extends BaseAdapter implements Filterable {
    
        private Context mContext;
        private LayoutInflater mInflater;
        private SparseBooleanArray mSparseBooleanArray;
        // from where do you get the data?
        private ArrayList> mAdapData = new ArrayList>();
        private ArrayList> mOriginalData = new ArrayList>();
    
        public MyCustomAdapter(Context mContext) {
            mInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mSparseBooleanArray = new SparseBooleanArray();
        }
    
        public ArrayList getCheckedItems() {
            ArrayList mTempArry = new ArrayList();
            for (int i = 0; i < mAdapData.size(); i++) {
                if (mSparseBooleanArray.get(i)) {
                    Map map = (Map) mAdapData
                            .get(i);
                    final String numbr = map.get("Phone");
                    mTempArry.add(numbr);
                }
            }
            return mTempArry;
        }
    
        @Override
        public int getCount() {
            return this.mAdapData.size();
        }
    
        public void addItem(String paramString1, String paramString2) {
            Map NameNumber = new HashMap();
            NameNumber.put("Name", paramString1);
            NameNumber.put("Phone", paramString2);
            mAdapData.add(NameNumber);
            mOriginalData.add(NameNumber);
            notifyDataSetChanged();
        }
    
        @SuppressWarnings("unchecked")
        public Object getItem(int paramInt) {
            return (ArrayList>) 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.adapters_specialfilterrow,
                        paramViewGroup, false);
                viewHolder.tvName = (TextView) paramView
                        .findViewById(R.id.textView1);
                viewHolder.tvNumber = (TextView) paramView
                        .findViewById(R.id.textView2);
                viewHolder.cb = (CheckBox) paramView.findViewById(R.id.checkBox1);
                paramView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) paramView.getTag();
            }
            viewHolder.cb.setTag(paramInt);
            viewHolder.cb.setOnCheckedChangeListener(null);
            viewHolder.cb.setChecked(mSparseBooleanArray.get(paramInt));
            viewHolder.cb.setOnCheckedChangeListener(mCheckedChangeListener);
            viewHolder.tvName.setTextColor(Color.BLACK);
            viewHolder.tvNumber.setTextColor(Color.BLACK);
            Map map = (Map) mAdapData.get(paramInt);
            final String name = map.get("Name");
            final String numbr = map.get("Phone");
            viewHolder.tvName.setText(name);
            viewHolder.tvNumber.setText(numbr);
            return paramView;
        }
    
        OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // get the item from the current mAdapData
                Map item = mAdapData.get((Integer) buttonView
                        .getTag());
                int position = -1;
                // see where is the item placed in the mOriginalData and use that
                // position
                for (int i = 0; i < mOriginalData.size(); i++) {
                    final Map tmp = mOriginalData.get(i);
                    if (tmp.get("Name").toLowerCase().equals(item.get("Name").toLowerCase())) {
                        position = i;
                        break;
                    }
                }
                mSparseBooleanArray.put(position, 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> mFilteredData = new ArrayList>();
    
                if (!TextUtils.isEmpty(constraint)) {
                    for (int i = 0; i < mAdapData.size(); i++) {
                        Map map = (Map) mAdapData
                                .get(i);
                        final String names = map.get("Name");
                        final String numbr = map.get("Phone");
                        if (names.toLowerCase().contains(
                                constraint.toString().toLowerCase())) {
                            Map FilNameNumber = new HashMap();
                            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>) fr.values;
                notifyDataSetChanged();
            }
        }
    }
    

提交回复
热议问题