在过滤listview时,过滤android中复选框的位置?

时间:2014-02-12 14:01:11

标签: android checkbox android-listview

我有一个自定义适配器,它由复选框组成。我已经通过EditText实现了过滤,我的过滤工作完全正常。当我过滤时,检查框的位置会发生变化,当我输入一个字母时,它的位置从零开始。以及它在orignal中的位置。

我说山姆是列表视图中第9位的名字。我在edittext中键入s,sam显示在第一行,我检查并清除了edittext,然后第一个复选框被选中而不是第9行。 我知道这是因为职位问题。 我想要的是当用户在Edittext中键入时,复选框也应该按其位置进行过滤。 我该如何实现这一目标? 请你帮忙。多天来坚持这个问题。

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);
        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();             


                }
        }


    }
}

0 个答案:

没有答案