I am developing an application which uses list view with check-boxes,consider there are 10 items
on list-view
items,And by default in that check boxes are checked,Until now every thing is working fine for me here is my problem,When I uncheck any check-box in list-view whole list-view need to be refresh.
Nikhil just keep in mind that defining a custom adapter is one time practice, once you define and understand it properly then you can customize any views like ListView, GridView, Gallery, Spinner. So go through the below answer properly.
For defining ListView with CheckBox (or any View), you have to define your own custom adapter. For defining custom adapter, follow below steps:
In your case,
<RelativeLayout>
<TextView>
<CheckBox>
</RelativeLayout>
public class MyListViewAdapter extends BaseAdapter
{
....
....
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
.......
.......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With