筛选列表视图中的Android未选中复选框

筛选列表视图中的Android未选中复选框,android,listview,android-listview,android-checkbox,Android,Listview,Android Listview,Android Checkbox,下面这段过滤listView的代码是当前代码,没有任何问题。但在搜索并选中复选框后,按android键盘上的BackSpace键清除要开始的字符和字符串,所有复选框均未选中,我不想拥有此功能,我想在搜索或清除可搜索编辑文本时选中listView public class ContactsAdapter extends ArrayAdapter<ContactListStructure> implements Filterable { private ArrayList<

下面这段过滤listView的代码是当前代码,没有任何问题。但在搜索并选中复选框后,按android键盘上的BackSpace键清除要开始的字符和字符串,所有复选框均未选中,我不想拥有此功能,我想在搜索或清除可搜索编辑文本时选中listView

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();
        }
    }
}
公共类ContactsAdapter扩展ArrayAdapter实现可过滤{
私有ArrayList项=新建ArrayList();
私家侦探作家;
私有名称过滤器;
公共联系人适配器(ArrayList数据){
超级(G.context、R.layout、发送短信、数据);
项目=数据;
originalList=新的ArrayList();
原始列表addAll(数据);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
ContactListStructure item=getItem(位置);
if(convertView==null){
convertView=G.充气机.充气(R.布局.发送短信,父项,错误);
支架=新的视图支架(convertView);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
持有人。填写(本、项目、位置);
返回视图;
}
私有静态类视图持有者{
私人复选框chk_name_mobile;
私人影像查看照片;
公共视图持有者(视图){
chk_name_mobile=(复选框)view.findviewbyd(R.id.chk_name_mobile);
photo=(ImageView)view.findviewbyd(R.id.photo);
}
公共空白填充(最终阵列适配器、最终ContactListStructure项、最终int位置){
chk_name_mobile.setText(item.name);
如果(已选中项){
chk_name_mobile.setChecked(true);
如果(item.photo!=null)photo.setImageBitmap(item.photo);
}否则{
chk_name_mobile.setChecked(false);
如果(item.photo==null)
setImageDrawable(G.context.getResources().getDrawable(R.drawable.user));
其他的
photo.setImageBitmap(item.photo);
}
chk_name_mobile.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
item.checked=true;
}否则{
item.checked=false;
}
}
});
}
}
@凌驾
公共过滤器getFilter(){
if(filter==null){
filter=新名称filter();
}
回流过滤器;
}
私有类NameFilter扩展了过滤器
{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
constraint=constraint.toString().toLowerCase();
FilterResults结果=新的FilterResults();
if(constraint!=null&&constraint.toString().length()>0)
{
ArrayList filteredItems=新的ArrayList();
for(int i=0,l=originalList.size();i
我有相同的,为什么会发生这种情况,我还附上了相同的源代码。因此,您可以从

获得它。我已经为您修改了代码。首先阅读并理解代码,然后在程序中使用:)

模型类:
包com.example.model;
公共类联系人列表结构{
字符串名;
布尔检查;
公共ContactListStructure(字符串名称,布尔检查){
超级();
this.name=名称;
this.check=检查;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共布尔值isCheck(){
退货检查;
}
公共void设置检查(布尔检查){
this.check=检查;
}
}
在主要活动中;
namesarrayList=新的Arraylist();
ContactListStructure-conliststruct;
对于(int i=0;i 0)
{
ArrayList filteredItems=新的ArrayList();
对于(int i=0,l=filteritemList.size();iModel 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();
        }
    }
}