Checkbox Gets Unchecked on Scroll in a Custom Listview

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 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;

}
}

CheckBox gets unchecked on scroll in a custom listview

getView() is called whenever a previously invisible list item needs to be drawn. Since you recreate itemChecked[] each time this method is called you will have the new checkbox unchecked and a different Array for each resulting View. (final in Java does not make that field unique like in C)
Simplest way to solve that is to make itemChecked a classmember and set / restore checkbox state based on that one.

public class MyListAdapter extends ArrayAdapter<Object> {
private final boolean[] mCheckedState;
private final Context mContext;

public MyListAdapter(Context context, int resource, int textViewResourceId, List<Object> objects) {
super(context, resource, textViewResourceId, objects);
mCheckedState = new boolean[objects.size()];
mContext = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// simplified to just a Checkbox
// ViewHolder and OnCheckedChangeListener stuff left out
CheckBox result = (CheckBox)convertView;
if (result == null) {
result = new CheckBox(mContext);
}
result.setChecked(mCheckedState[position]);
return result;
}
}


Related Topics



Leave a reply



Submit