janki janki - 1 month ago 6
Android Question

check box state not working properly

I am using a

ListView
that has a check box. After click on a check box and scroll, check box state is not working properly.

I have tried look at a similar StackOverflow question
however these solutions are not working for me.

Here is my
Adapter
code:

private class CustomerEventsAdapter extends BaseAdapter {
private Context myContext;
private ArrayList<Events> myEventArrayList;

private LayoutInflater mInflater;
private Boolean isSelected = false;

public EventsAdapter(Context ctx, ArrayList<Events> theArrayListEvents) {
this.myContext = ctx;
this.myEventArrayList = theArrayListEvents;
mInflater = LayoutInflater.from(ctx);

}

@Override
public int getCount() {
return myEventArrayList.size();
}

@Override
public Object getItem(int i) {
return myEventArrayList.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (view == null) {
view = mInflater.inflate(R.layout.event_item_list, null);
final ViewHolder holder = new ViewHolder();


holder.myCheckBoxCustomerEventItem = (CheckBox) view.findViewById(R.id.checkBoxCustomerEventItem);





holder.myCheckBoxCustomerEventItem.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Events events = myEventArrayList.get(i);
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
holder.myCheckBoxCustomerEventItem.setChecked(true);
eventsInfo.setMyIsSelected(true);
} else {
holder.myCheckBoxCustomerEventItem.setChecked(false);
eventsInfo.setMyIsSelected(false);
}
//case 2

}
});




view.setTag(holder);

}

if (view != null) {
ViewHolder holder = (ViewHolder) view.getTag();
Events eventsInfo = myEventArrayList.get(i);

boolean isChecked = eventsInfo.getMyIsSelected();


if (isChecked) {
holder.myCheckBoxCustomerEventItem.setChecked(true);


} else {
holder.myCheckBoxCustomerEventItem.setChecked(false);


}
}

return view;
}
}


Thanks.

Answer

The basic rule of implementing getView method in an adapter is to inflate the view only when there is not view to resuse, but update all the views everytime getView is being called.

You are not setting the Checkbox properties in case convertView == null. CheckBoxes have a OnCheckedChangeListener that you can use to identify state changes, rather than handling clicks on it.

Change yourgetView method as:

@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
    View view = convertView;
    if (view == null) {
        view = mInflater.inflate(R.layout.event_item_list, null);
        ViewHolder holder = new ViewHolder();

        holder.myCheckBoxCustomerEventItem = (CheckBox) view.findViewById(R.id.checkBoxCustomerEventItem);

        holder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                myEventArrayList.get(i).setMyIsSelected(isChecked);
            }
        });

        view.setTag(holder);
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    Events eventsInfo = myEventArrayList.get(i);

    boolean isChecked = eventsInfo.getMyIsSelected();

    holder.myCheckBoxCustomerEventItem.setChecked(isChecked);

    return view;
}
Comments