【问题标题】:Android unchecked checkbox in filtering ListView过滤ListView中的Android未选中复选框
【发布时间】:2014-12-04 09:49:27
【问题描述】:

下面的过滤listView的代码是正确的,没有任何问题。但是在搜索并选中复选框并按 android 键盘上的 BackSpace 以清除字符和字符串以启动后,所有复选框都未选中,我不想拥有此功能,我想在搜索或清除可搜索的 EditText 时检查 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();
        }
    }
}

【问题讨论】:

    标签: android listview android-listview android-checkbox


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢先生。我收到此错误:Error:(42, 47) java: cannot find symbol symbol: method isCheck()
    • 我添加了一个示例模型类。不要复制粘贴所有内容。只需使用我在那里使用的逻辑即可。 P.S:不要叫我先生。我是初学者; :)
    • 项目未正确检查。例如,在签入一项后,一些项目被检查
    • 我已经更新了代码。阅读并理解逻辑,然后在您的程序中使用它:)
    【解决方案2】:

    我有同样的想法,为什么会发生这种情况,我也附上了相同的来源。所以,你可以从ListView with CheckBox Scrolling Issue得到它

    【讨论】:

      猜你喜欢
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 2015-08-23
      相关资源
      最近更新 更多