简体   繁体   中英

Checkbox gets unchecked in listview android?

I know there are many solutions provided to this issue, But I am not exactly the proper solution to this, when a checkbox is checked I am assigning a ID to it,and adding it to a array, but when scroll down, the checkbox gets unchecked ,but the ID is stored in array,I want to keep the checkbox checked if it is checked.

My adapter class is.

public class sendivitesadapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;
    private String[] udis;
    private final boolean[] mCheckedState;
    qrusers qrus;

    private ItemsFilter mFilter;

    public sendivitesadapter(Context context,ArrayList<Item> items,String[]udis) {
        super(context, 0,items);

        this.context = context;
        mCheckedState = new boolean[items.size()];
        this.qrusers =(qrusers) context;
        this.items = items;
        this.udis=uid;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Item getItem(int position) {
        return super.getItem(position);
    }

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

        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                sendItem ei = (sendItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);
                final TextView title = (TextView)v.findViewById(R.id.contactname);
                final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                 checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
                //checkBox.setTag(position);
                //items.addAll(uid);

               checkBox.setTag(udis[position]);

               checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        s = (String)buttonView.getTag();
                        Log.e("IDDDDDDDD", s);

                        userid.add(s);
                        Log.e("Array", userid.toString());
                    } else {
                        s = (String)buttonView.getTag();
                        userid.remove(s);

                    }
                    SharedPreferences app_preferences = PreferenceManager
                            .getDefaultSharedPreferences(qrusers.this);
                    SharedPreferences.Editor editor = app_preferences.edit();

                    editor.putString("userid", TextUtils.join(",", userid));
                    editor.commit();
                }
            });


                //    Log.e("IDDDDDDD", text);
                    //checkBox.setTag("12");
                /*  checkBox.setOnClickListener(new OnClickListener() {


                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                        if(checkBox.isChecked()){
                             s= (String)v.getTag();
                            Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                        userid.add(s);

                        Log.e("Array", userid.toString());

                        }
                        else{
                        s=(String)v.getTag();
                        userid.remove(s);
                        }

                        }
                    });*/
                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }

如果要存储复选框的状态,则必须在内部ListView或正在使用的任何结构以及扩展适配器中的所谓items中进行更新,并且不要忘记调用notifyDataSetChanged()之后。

This question has your answer. You have to maintain a separate structure to check/uncheck the checkboxes.

ListView Viewholder checkbox state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM