0

I am programming my first android app. For test purposes I show a list of objects (class cElement) at the mainactivity.

public class cElement {
private Integer Id;
private String Name;
private String Description;
private Boolean Checked;

....
}

The ListView is linked with a custom adapter to the list. The custom adapter extends the BaseAdapter and I inflate the layout of each element from the list.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
<TextView
        android:id="@+id/id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@+id/standard"
        android:textSize="20dp"
        android:layout_weight="1">
</TextView>
<TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@+id/standard"
        android:textSize="20dp"
        android:layout_weight="2">
</TextView>
<TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@+id/standard"
        android:textSize="20dp"
        android:layout_weight="3">
</TextView>
<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:id="@+id/checkBox"/>
</LinearLayout>

First question: Now I would like to check all CheckBoxes in one step with a button. What is good style?

  1. I update the list directly and call the notifyDataSetChanged method of the adapter
  2. I create a method checkall() within the adapter and call it from outside.

Second question: I would like to know which CheckBox is checked. So I create an eventhandler setOnClickListener and save the position in a variable within the adapter. Is this the right way?

1 Answers1

0

First question: Now I would like to check all CheckBoxes in one step with a button. What is good style?

As it seems you have List which contains boolean Checked for status, you should update all objects of list and notify list again on click of checkAll button.

Second question: I would like to know which CheckBox is checked. So I create an eventhandler setOnClickListener and save the position in a variable within the adapter. Is this the right way?

You should implement listener from fragment/activity, when user clicks on any check box, from adapter, you will get click event in fragment/activity, over there, you update your list item for checked status.

So, in your fragment/activity:

new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.getTag()
            }
        }

And in adapter, set tag for each item with item id/ item object, so when user clicks on any item, you will come to know that item object / item id.

Kinnar Vasa
  • 397
  • 1
  • 9