包含复选框的自定义适配器以及EditText中的过滤功能的教程?

时间:2014-02-11 10:33:07

标签: android listview checkbox

我是Android开发的新手,并为我的问题搜索了很多解决方案,但我没有得到它。

我想创建一个带复选框的自定义适配器。我做了。我的列表视图包含复选框。

我添加了过滤功能,也运行正常。

但是当我过滤列表并选中复选框并清除edittext时,第一个复选框被选中,即使我搜索了字母x并选择名称x的复选框,复选框也会在列表视图上的第一个名称上勾选而不是x。

这是我的班级。

public class CustomAdapter extends ArrayAdapter<Custom> implements Filterable{
    private ArrayList<Custom> entries;
    private Activity activity;
    private Context context;
    private qrusers qrusers;
     Custom data;
    CustomFilter customFilter;
    private String[] udis;
    ArrayList<String> userid= new ArrayList<String>();
    boolean[] checkBoxState;
     private ArrayList<Custom> orignalvalues;
    LayoutInflater vi;
    public CustomAdapter(Context context, ArrayList<Custom> entries,String []udis,ArrayList<Custom> orignalvalue) {
        super(context,0,entries);
        this.entries = entries;
        this.context= context;
        orignalvalues = new ArrayList<Custom>(entries);
        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 int getCount() {
        // TODO Auto-generated method stub

        return entries.size();
    }

    @Override
    public Custom getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
        }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        final Custom i = entries.get(position);
        final Custom t = orignalvalues.get(position);
       // ((ListView)parent).setItemChecked(position, true);
        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);
            data = getItem(position);

            holder.chek.setOnCheckedChangeListener(null); //important 
            holder.chek.setChecked(data.isChecked());


            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;
                    data.setChecked(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());
                }
                SharedPreferences app_preferences = PreferenceManager
                        .getDefaultSharedPreferences(context);
                SharedPreferences.Editor editor = app_preferences.edit();

                editor.putString("userid", TextUtils.join(",", userid));
                editor.commit();
                }

        }
        );
        Custom ei = (Custom)i;
        if (ei != null) {
            holder.item1.setText(ei.getcustomBig());
            holder.item2.setText(ei.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) {
                 entries = new ArrayList<Custom>(orignalvalues);
                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);
                 }
                results.count = newValues.size(); 
                results.values = newValues;
                Log.e("NEW VALUES", newValues.toString());
            }

            return results;
        }

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

                } else {
                    Log.e("publish result", results.toString());
                    @SuppressWarnings("unchecked")
                    ArrayList<Custom> listi = (ArrayList<Custom>)results.values;
                    ArrayList<Custom> itemsList = new ArrayList<Custom>(listi);
                    entries =itemsList;              
                    notifyDataSetChanged();             


                }
        }


    }
}

1 个答案:

答案 0 :(得分:0)

您最好实现可检查的布局并使用ListView.setChoiceMode方法。 你可以在这里找到例子:

http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

http://adanware.blogspot.ru/2012/04/android-multiple-selection-listview.html