[Android] [Java] Manage the state of CheckBox of ListView

It was packed for about 2 months, so a memorandum

Execution environment

IDE:Android Studio 3.4 Language: Java

Xperia XZ3 (used for debugging the actual machine) OS is Android 9 (API Level 28)

What do you want to do

Prevent the CheckBox in the ListView from changing its check state by scrolling.

Solution

In the getView method that processes the elements displayed in ListView, re-paste the listener of CheckBox of ListView and use the position of the argument of getView in the onCheckedChanged method to get the item again.

Although it is not shown in the code below ArrayAdapter extends, I'm using the ViewHolder pattern.

The following is a successful example.

ListAdapter.java


//ListItem is a self-made class of elements to be displayed in ListView.
private ListItem item;
//Omitted ~~~~~~~~~~~~
public View getView (final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        //If there is nothing in convertView, create a new one.
        convertView = mInflater.inflate(mResId, parent, false);

        //Get the element of each item in the list.
        holder = new ViewHolder();
        holder.isActive = convertView.findViewById(R.id.alarm_active);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    //Get an instance of the item to display in the list view.
    item = getItem(position);

    //Peel off the listener.
    holder.isActive.setOnCheckedChangeListener(null);

    //Item obtained from position on the element(Instance of homebrew class)Is assigned to the element.
    holder.isActive.setChecked(item.isActive());

    //Paste the listener.
    holder.isActive.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged (CompoundButton cb, boolean isChecked) {
                ListItem listItem = getItem(position);
                //Null check for warning
                if (listItem != null){
                    listItem.setActive(isChecked);
                }
            }
        });

    return convertView;
}

The important thing here is in the ** onCheckedChanged ** method in the anonymous class ListItem listItem = getItem(position); It is a process called.

This process is to reflect the check status in the list element (self-made class), Delete this part just because it is already done like ʻitem = getItem (position); If you do something like ʻitem.setActive (isChecked);, it will not work properly.

Recommended Posts

[Android] [Java] Manage the state of CheckBox of ListView
Organizing the current state of Java and considering the future
<Android> Change the background color of the List row of ListView
Monitor the internal state of Java programs with Kubernetes
[Java] Delete the elements of List
About the basics of Android development
[Java version] The story of serialization
The origin of Java lambda expressions
[Android] Dynamically set the height of ListView with copy (for beginners)
[Android] Implementation of side-scrolling ListView using RecyclerView
Understand the basics of Android Audio Record
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
Compare the elements of an array (Java)
What wasn't fair use in the diversion of Java APIs on Android
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
Looking back on the basics of Java
Output of the book "Introduction to Java"
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
Summarize the life cycle of Java objects to be aware of in Android development
The story of low-level string comparison in Java
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
About the description order of Java system properties
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
Manage the version of Ruby itself with rbenv
[Java] Be careful of the key type of Map
Feel the passage of time even in Java
The story of tuning android apps with libGDX
Calculate the similarity score of strings with JAVA
Try the free version of Progate [Java II]
[Java / Kotlin] Resize considering the orientation of the image
I touched on the new features of Java 15
The date time of java8 has been updated
[Java] How to get the authority of the folder
Story of test automation using Appium [Android / java]
Import files of the same hierarchy in Java
First touch of the Files class (or Java 8)
Java Welcome to the Swamp of 2D Arrays
Try the free version of Progate [Java I]
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
[Java] Overview of Java
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
CI the architecture of Java / Kotlin applications with ArchUnit
How to write Scala from the perspective of Java
Please note the division (division) of java kotlin Int and Int