在过滤ListView时,Android未选中复选框

时间:2014-12-04 09:49:28

标签: android listview android-listview android-checkbox

以下代码用于过滤listView是当前的,没有任何问题。但搜索并勾选复选框后按Android键盘上的BackSpace清除字符和字符串开始,所有复选框未选中,我不想拥有此功能,我想在搜索时检查listView或清除可搜索的EditText。

public class ContactsAdapter extends ArrayAdapter<ContactListStructure>  implements Filterable {
    private ArrayList<ContactListStructure> item = new ArrayList<ContactListStructure>();
    private ArrayList<ContactListStructure> originalList;
    private NameFilter filter;
    public ContactsAdapter (ArrayList<ContactListStructure> data) {
        super(G.context, R.layout.send_sms, data);
        item = data;
        originalList = new ArrayList<ContactListStructure>();
        originalList.addAll(data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        ContactListStructure item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.send_sms, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.fill(this, item, position);
        return convertView;
    }

    private static class ViewHolder {
        private  CheckBox   chk_name_mobile;
        private  ImageView  photo;

        public ViewHolder(View view) {
            chk_name_mobile = (CheckBox)  view.findViewById(R.id.chk_name_mobile);
            photo           = (ImageView) view.findViewById(R.id.photo);
        }

        public void fill(final ArrayAdapter<ContactListStructure> adapter, final ContactListStructure item, final int position) {
            chk_name_mobile.setText ( item.name );
            if( item.checked ){
                chk_name_mobile.setChecked( true );
                if( item.photo != null ) photo.setImageBitmap(item.photo);
            }else{
                chk_name_mobile.setChecked( false );
                if( item.photo == null )
                    photo.setImageDrawable( G.context.getResources().getDrawable(R.drawable.user) );
                else
                    photo.setImageBitmap(item.photo);
            }

            chk_name_mobile.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                   @Override
                   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if( isChecked ){
                   item.checked = true;
               }else{
                   item.checked = false;
               }
                   }
            });
        }
    }
    @Override
    public Filter getFilter() {
        if (filter == null){
            filter  = new NameFilter();
        }
        return filter;
    }
    private class NameFilter extends Filter
    {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<ContactListStructure> filteredItems = new ArrayList<ContactListStructure>();
                for(int i = 0, l = originalList.size(); i < l; i++)
                {
                    ContactListStructure nameList = originalList.get(i);
                    if(nameList.name.toString ().contains(constraint)) {
                        filteredItems.add ( nameList );
                    }
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            }
            else
            {
                synchronized(this)
                {
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {
            item = (ArrayList<ContactListStructure>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = item.size(); i < l; i++)
                add(item.get(i));
            notifyDataSetInvalidated();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

I have modified the code for you.   First read and understand the code, then use it in your program :)       



Model Class:

package com.example.model;

public class ContactListStructure {
String name;
boolean check;

public ContactListStructure(String name, boolean check) {
    super();
    this.name = name;
    this.check = check;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public boolean isCheck() {
    return check;
}
public void setCheck(boolean check) {
    this.check = check;
}


}


In main activity;

namesarrayList=new Arraylist<ContactListStructure >();

ContactListStructure conliststruct;
for(int i=0;i<namesarray.length();i++)
{
    conliststruct=new ContactListStructure(namesarray[i], false);
    namesarrayList.add(conliststruct);
}
ContactsAdapter adapter=new ContactsAdapter (namesarrayList);
listview.setAdapter(adapter);





Adapter Class:

public class ContactsAdapter extends ArrayAdapter<ContactListStructure>  implements Filterable {
    private ArrayList<ContactListStructure> filteritemList = new ArrayList<ContactListStructure>();
    private ArrayList<ContactListStructure> originalList;
    private NameFilter filter;
    private  CheckBox   chk_name_mobile;
    private  ImageView  photo;



    public ContactsAdapter (ArrayList<ContactListStructure> data) {
        super(G.context, R.layout.send_sms, data);

        this.originalList = new ArrayList<ContactListStructure>();
        originalList.addAll(data);
        this.filteritemList = new ArrayList<ContactListStructure>(originalList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos=position;

        ContactListStructure item = (ContactListStructure)getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.send_sms, parent, false);


        } else {

        }

        chk_name_mobile = (CheckBox)  convertView.findViewById(R.id.chk_name_mobile);
        photo           = (ImageView) convertView.findViewById(R.id.photo);
        chk_name_mobile.setText(item.getName().toString());
        chk_name_mobile.setChecked(item.isCheck());
        chk_name_mobile.setTag(item);

     chk_name_mobile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v;
                 ContactListStructure itemobj=(ContactListStructure) cb.getTag();


                if (originalList.get(Integer.valueOf(pos)).isCheck()) {
                    cb.setSelected(false);
                    originalList.get(Integer.valueOf(pos)).setCheck(false);
                    notifyDataSetChanged();
                } else {
                    cb.setSelected(true);
                    originalList.get(Integer.valueOf(pos)).setCheck(true);      
                    notifyDataSetChanged();
                }

                //region.setCheck(chkItem.isChecked());
            }
        });
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (filter == null){
            filter  = new NameFilter();
        }
        return filter;
    }
    private class NameFilter extends Filter
    {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<ContactListStructure> filteredItems = new ArrayList<ContactListStructure>();
                for(int i = 0, l = filteritemList.size(); i < l; i++)
                {
                    ContactListStructure nameList = filteritemList.get(i);
                    if(nameList.name.toString ().contains(constraint)) {
                        filteredItems.add ( nameList );
                    }
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            }
            else
            {
                synchronized(this)
                {
                    result.values = filteritemList;
                    result.count = filteritemList.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {
            originalList = (ArrayList<ContactListStructure>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = originalList.size(); i < l; i++)
                add(originalList.get(i));
            notifyDataSetInvalidated();
        }
    }
}

答案 1 :(得分:0)

我有同样的原因,为什么会发生这种情况,而且我也附加了相同的来源。所以,你可以从ListView with CheckBox Scrolling Issue

获得它