-3

listView with dual CheckBox Image

Am new in Android
I have two checkbox in a list I want to check one at time and unable the other one

my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/student_name"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:padding="20dp"/>

    <CheckBox
        android:id="@+id/present_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:duplicateParentState="false" />

    <CheckBox
        android:id="@+id/absent_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

list.xml

<LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/names_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </LinearLayout
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
xcde
  • 3
  • 3

4 Answers4

1

You can use RadioGroup instead Checkbox then add two radioButton to radioGroup.

0

for your purpose, just use RadioButton, check this tutorial.

also, see this answer on how to do that using RadioButton and RadioGroup

Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
0

Radio buttons are designed exactly for this case. But if you really want to use the checkboxes, when the click event happens for the item in the list on a checkbox, set the other checkbox checked state with the reverse of the current checkbox. So if you receive an event like this pseudo-code:

onCheckChanged(CheckBox oneCheckbox, boolean isChecked){
    otherCheckbox.setChecked(!isCchecked);
}
Tudor Lozba
  • 736
  • 1
  • 6
  • 9
0

When a row in your listView is clicked you must call notifyDataSetChanged(), so that all views get updated.

listView.setOnItemClickListener((parent, view, position, id) -> {
        
// do something
    
notifyDataSetChanged()
});
Franco
  • 669
  • 2
  • 8
  • 23