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
How to Create a User-Defined Function in SQLite
Android 2.1 Programmatically Unmount Sdcard
How to Reference Style Attributes from a Drawable
Getexternalstoragepublicdirectory Deprecated in Android Q
Build and Install Unsigned APK on Device Without the Development Server
How to Programmatically Turn Off Wifi on Android Device
How to Strip or Escape HTML Tags in Android
How to Get the APK of an Installed App Without Root Access
How to Handle Imeoptions' Done Button Click
How to Convert Coordinates of the Image View to the Coordinates of the Bitmap
Listview with Clickable/Editable Widget
Customize Android Facebook Login Button
Filtering Listview with Custom (Object) Adapter
How to Create Edittext with Rounded Corners
Non-Scrollable Listview Inside Scrollview
"Native Typeface Cannot Be Made" Only for Some People
How to Create Standard Borderless Buttons (Like in the Design Guideline Mentioned)