在listview上过滤自定义适配器在android中无法正常工作?

时间:2014-02-05 09:56:03

标签: android listview filtering android-filterable

我有一个数组适配器,如下所示。我已经实现了Filterable,但是当我输入EditText时也没有任何反应。

public class CustomAdapter extends ArrayAdapter<Custom> implements Filterable{
    private ArrayList<Custom> entries;
    private Activity activity;
    private Context context;
    private qrusers qrusers;
    CustomFilter customFilter;
    private String[] udis;
    ArrayList<String> userid= new ArrayList<String>();
    boolean[] checkBoxState;
    LayoutInflater vi;
    public CustomAdapter(Context context, ArrayList<Custom> entries,String []udis) {
        super(context,0,entries);
        this.entries = entries;
        this.context= context;
        this.udis=udis;
        checkBoxState=new boolean[entries.size()];
        this.qrusers =(qrusers) context;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder{
        public TextView item1;
        public TextView item2;
        public CheckBox chek;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {

            v = vi.inflate(R.layout.checkboxlist, null);
            holder = new ViewHolder();
            holder.item1 = (TextView) v.findViewById(R.id.contactname);
            holder.item2 = (TextView) v.findViewById(R.id.companyname);
            holder.chek=(CheckBox)v.findViewById(R.id.checboxlist);



            v.setTag(holder);


        }

        else{
            holder=(ViewHolder)v.getTag();


        }
        holder.chek.setChecked(checkBoxState[position]);
        holder.chek.setTag(udis[position]);
        holder.chek.setOnClickListener(new OnClickListener() {
            private String s;
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()){
                    checkBoxState[position]=true;
                    //  isChecked=true;
                    s=(String)v.getTag();
                    Log.e("IDDDDDDDD", s);

                    userid.add(s);
            Log.e("ADDED ID", userid.toString());
            }
                else{
                    checkBoxState[position]=false;
                    s=(String)v.getTag();
                    userid.remove(s);
                    Log.e("Removed ID", userid.toString());
                }
                }

        }
        );
        final Custom custom = entries.get(position);
        if (custom != null) {
            holder.item1.setText(custom.getcustomBig());
            holder.item2.setText(custom.getcustomSmall());

        } 
        return v;
        }
    public Filter getFilter() {
        if (customFilter == null) {
            customFilter = new CustomFilter();
        }
        return customFilter;
    }


    public ArrayList<String> getUserid() {
        return userid;
    }
    public void setUserid(ArrayList<String> userid) {
        this.userid = userid;
    }
    private class CustomFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(constraint == null || constraint.length() == 0) {
                results.values=entries;
                 results.count = entries.size();

            }
            else{

                ArrayList<Custom> newValues = new ArrayList<Custom>();
                for (Custom i : entries){

                     if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                            newValues.add(i);
                 }
            }
            // TODO Auto-generated method stub
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
 if (results.count == 0){

                 notifyDataSetInvalidated();
             }
             else{

                 @SuppressWarnings("unchecked")
                ArrayList<Custom> lst = (ArrayList<Custom>)results.values;
                 ArrayList<Custom> itemsList = new ArrayList<Custom>(lst);
                 //this.items=mItems;
                entries =itemsList;              
                notifyDataSetChanged();
             }

        }


    }
}

在我的活动中,我正在使用这段代码。

usersearch1.addTextChangedListener(new TextWatcher(){

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {


                    s=usersearch1.getText().toString();
                    adapter.getFilter().filter(s);
                    Log.e("ALPHABETS", usersearch1.getText().toString());
                    //sendadapter.getFilter().filter(s);
                }

1 个答案:

答案 0 :(得分:2)

您的publishResults()方法应如下所示:

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    if (results.count == 0){
        notifyDataSetInvalidated();
    } else {
        @SuppressWarnings("unchecked")
        ArrayList<Custom> lst = (ArrayList<Custom>)results.values;
        ArrayList<Custom> itemsList = new ArrayList<Custom>(lst);

        entries = itemsList;              
        clear();
        for (Custom item : entries) {
            add(item);
        }
    }
}