在android中过滤后,Checkbox失去了它的状态?

时间:2014-02-10 12:37:27

标签: android checkbox android-arrayadapter

我有一个自定义适配器类,如下所示,它由复选框组成。我使用过滤和settag方法用于存储uids,如下面的代码所示。

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


                }
        }


    }
}

enter image description here

这就是我的活动的样子。现在当我过滤清单时。

enter image description here

选中复选框并将EditText设为空白,选中第一个复选框。即阿米特。

现在我有两个问题。

1.当用户在过滤后检查同一个复选框并返回到orignal列表时,它应该与UID一起保持选中状态。

2.过滤时,我已将uid指定为holder.chek.setTag(udis [position]); 但是当实际过滤时,它会获取位置零的id,而不是受尊重的复选框。

我该怎么做?

0 个答案:

没有答案