Checkbox Unchecked When I Scroll Listview on Android

CheckBox will uncheck and check when scrolling down the listview

I guess the problem is not using holder

make a bean and adapter like @Athira says

then inside adapter in getView try this

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;

if (view == null) {
LayoutInflater inflater=LayoutInflater.from(context);
view=inflater.inflate(R.layout.phone_list_item,viewGroup,false);
holder.phone1= (TextView) view.findViewById(R.id.phone1);
holder.name1= (TextView) view.findViewById(R.id.name1);
holder.tick= (CheckBox) view.findViewById(R.id.tick);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.phone1.setText(contactModels.get(i).getPhone());
holder.name1.setText(contactModels.get(i).getName());
if(contactModels.get(i).isSel())
{
holder.tick.setSelected(true);
}
else
{
holder.tick.setSelected(true);
}
holder.tick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
contactModels.get(i).setSel(isChecked);
notifyDataSetChanged();
}
});
return view;
}
public class ViewHolder {
TextView phone1,name1;
CheckBox tick;

}
}

Custom ListView always gets unchecked after scrolling over

try this....i have modified your code

try this....



if(rowciew==null){
ViewHolder viewholder;
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);

viewholder=new ViewHolder();

viewholder.textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);

viewholder.checkBoxItem = (CheckBox)rowView.findViewById(R.id.checkboxList);

rowView.setTag(viewholder);
}else{
viewholder=(ViewHolder) rowview.getTag();
}
textView_name.setText(item_name[position]);

// Row View got click
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewholder.checkBoxItem.isChecked()==true){
viewholder.checkBoxItem.setChecked(false);

}
else viewholder.checkBoxItem.setChecked(true);
}
});

// Return row view
return rowView;

private class ViewHolder {
TextView textView_name;
CheckBox checkBoxItem;
}

}

CheckBox Checked or UnChecked When Scrolling Listview in Android

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.



Related Topics



Leave a reply



Submit