Checkbox check lost on listview scroll in android

Hi,

I am going to share a solution of well known issue in android listview. In list view when we scroll, the value of checkbox that are not visible on screen will lost. In order to save the values of all checkboxes in list view we need to update the adapter on checkbox click. Please find the example below.

In main_activity.xml

<ListView
    android:id="@+id/lvCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="afterDescendants"
    android:divider="#c8c8c8"
    android:dividerHeight="1dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollingCache="true"
    android:visibility="visible" />

In item_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/chkID"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/llText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/chkID"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="1dp"
            android:paddingLeft="10dp"
            android:paddingTop="1dp"
            android:textColor="@color/darktext"
            android:textSize="15sp"
            android:textStyle="bold"
            android:typeface="sans" />

    </LinearLayout>

</RelativeLayout>

In Adapter.java

public class CustomAdapter extends BaseAdapter {
    private static ArrayList<FormListItem> itemList;
    private Activity activity;
    private Context ctx;
    private LayoutInflater l_Inflater;
    private int lastFocussedPosition = -1;
    private Handler handler = new Handler();


    public CustomAdapter(Activity a, Context context, ArrayList<CustomListItem> results) {
        itemList = results;
        activity = a;
        l_Inflater = LayoutInflater.from(context);
        ctx = context;
    }

    public int getCount() {
        return itemList.size();
    }

    public Object getItem(int position) {
        return itemList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_view, null);

            holder = new ViewHolder();
            holder.chk = (CheckBox) convertView.findViewById(R.id.chkID);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
           
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chk.setChecked(itemList.get(position).getChk());
        holder.txtTitle.setText(itemList.get(position).getTitle());
        holder.chk.setTag(position);

        holder.chk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox c = (CheckBox)v.findViewById(R.id.chkID);
                Boolean chk = c.isChecked();
                Integer pos = (Integer)v.getTag();
                itemList.get(pos).setChk(chk);

            }
        });




        return convertView;
    }


    static class ViewHolder {
        CheckBox chk;
        TextView txtTitle;
     
    }
}

Checkbox setOnClickListener will change the adapter value on checkbox click. and it will solve the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *