Filtering custom adapter returning incorrect results

前端 未结 3 1360
盖世英雄少女心
盖世英雄少女心 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:33

    in the getView() method , you didn't update the UI to have the values of the filtered results . so it returned views with old results .

    just before return paramView , update the view's data to what you have in the filtered results , and remove it from the preparation of the viewHolder , since this is called only a few times .


    EDIT: here's my correction of the code . i think it will fix it:

    @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.tvName.setTextColor(Color.BLACK);
        viewHolder.tvNumber.setTextColor(Color.BLACK);
        paramView.setTag(viewHolder);
        viewHolder.cb.setOnCheckedChangeListener(mCheckedChangeListener);
    } else {
        viewHolder = (ViewHolder) paramView.getTag();
    }       
    viewHolder.cb.setTag(paramInt);
    viewHolder.cb.setChecked(mSparseBooleanArray.get(paramInt));
    
    
    Map map = (Map) mAdapData.get(paramInt);
    final String name = map.get("Name").toString();
    Log.e("Name", name);
    final String numbr = map.get("Phone").toString();
    Log.e("Number", numbr);
    viewHolder.tvName.setText(name);
    viewHolder.tvNumber.setText(numbr);
    
    return paramView;
    }
    

    EDIT: here's a sample of how i handled the filtering :

    @Override
    public Filter getFilter()
      {
      final Filter filter=new Filter()
        {
          @Override
          protected FilterResults performFiltering(final CharSequence constraint)
            {
            _lastFilterConstraint=constraint;
            if(TextUtils.isEmpty(constraint))
              return null;
            final String constraintToCheck=constraint.toString().toLowerCase(Locale.getDefault());
            final FilterResults results=new FilterResults();
            // <- here i do the filtering itself and later put the results into "results"
            results.values=values;
            results.count=values.size();
            return results;
            }
    
          @SuppressWarnings("unchecked")
          @Override
          protected void publishResults(final CharSequence constraint,final FilterResults results)
            {
            _filteredItems==null ? null : (ArrayList)results.values;
            notifyDataSetChanged();
            }
        };
      return filter;
      }
    
    @Override
    public int getCount()
      {
      if(_filteredItems!=null)
        return _filteredItems.size();
      return _originalItems.size();
      }
    
    @Override
    public Item getItem(final int position)
      {
      if(_filteredItems!=null)
        {
        if(position<_filteredItems.size())
          return _filteredItems.get(position);
        return null;
        }
      if(position<_originalItems.size())
        return _originalItems.get(position);
      return null;
      }
    
    @Override
    public View getView(final int position,final View convertView,final ViewGroup parent)
      {
      final View inflatedView;
      final ViewHolder viewHolder;
      if(convertView==null)
        {
        //<- here i inflate the view into the inflatedView
        }
      else
        {
        inflaterView=convertView;
        viewHolder=(ViewHolder)inflaterView.getTag();
        }
      // <- here i use getItem and update the view according to its data .
      return inflaterView;
      }
    

提交回复
热议问题