简体   繁体   中英

CheckBox Checked or UnChecked When Scrolling Listview in Android

I created a Listview with Textview and Checkbox. Firstly I keep all item is Checked default.

But when I uncheck the checkbox and scroll it down to uncheck some other items in the list view, the older ones are checked. Please help me with my code.

This is my code for Adapter Class.

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

try {
    ViewHolder holder = new ViewHolder();

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    if (convertView == null) {

        convertView = layout_inflater.inflate(R.layout.dashbordmenu_listadapter, null);
        holder.txtMenutitle = (TextView) convertView.findViewById(R.id.txtPersonList);
        holder.checkAppList = (CheckBox) convertView.findViewById(R.id.checkPersonList);
        convertView.setTag(holder);
        convertView.setTag(R.id.txtPersonList, holder.txtMenutitle);
        convertView.setTag(R.id.checkPersonList, holder.checkAppList);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.checkAppList.setTag(position);
    holder.txtMenutitle.setText(arrayListDashboard1.get(position).getMenuTitle());
    holder.checkAppList.setChecked(arrayListDashboard1.get(position).isSelected());
    holder.checkAppList.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int selectPosition = (int) buttonView.getTag();
            if(buttonView.isChecked())
            {
                selectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
                Log.d("TEST","selectedAppId = "+selectedAppId);

            } else {
                notSelectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
                  Log.d("TEST","notSelectedAppId = "+notSelectedAppId);
            }
        }
    });

} catch (Exception e) {
    e.printStackTrace();
}
return convertView;
}
public class ViewHolder {
   TextView txtMenutitle;
   public CheckBox checkAppList;
  }

First Method:

Add a boolean field to your model. set it as default true. if checkbox unchecked make the boolean field as false. update your checkbox according to the boolean field.

Second Method:

using SparseBooleanArray , In your adapter initialize SparseBooleanArray. It will hold the positon and boolean value. So you can keep your check box value.

SparseBooleanArray Document:

Unlike a normal array of booleans, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Booleans, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping.

Third Method:

As LunarWatcher Said, Simply Initialize ArrayList of Booleans and update your check boxes.

在您的模型中,采用一个布尔值来保持复选框是否被选中的记录,并在绑定视图时使用它

RecylerView re-uses views while scrolling so you'll have to main the state of the checkbox for each list item. In BindViewHolder you'll check the state for the position and set the state of the checkbox accordingly.

Set a boolean variable 'isCheck' to your model class and set it as default false.change this according to checkbox value changed as true or false. and also check in checkbox change event isCheck is true or false.

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