How to use ExpandableListView in Android Studio

Introduction

It will be a memo of the list that can be collapsed.

code

string.xml


<resources>
    <string name="app_name">ExpandableListView</string>
</resources>

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/exListView"></ExpandableListView>

</LinearLayout>

MainActivity.java


package com.websarva.wings.android.expandablelistview;

import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    List<Map<String, String>> parentList;
    List<List<Map<String, String>>> childList;
    List<Map<String, String>> childDataList;
    Map<String, String> childtData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parentList = new ArrayList<>();
        childList = new ArrayList<>();

        //Generate what to display in the parent list
        setParentList("parentKey", "Parent 1");
        setParentList("parentKey", "Parent 2");

        //Generate content to display in child list
        String firstItemName1[] = {"Child 1", "Child 2"};
        String secondItemName1[] = {"Child 1st stage", "Child 2nd stage"};
        setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);

        String firstItemName2[] = {"Child A", "Child B", "Child C"};
        String secondItemName2[] = {"Child A stage", "Child B stage", "Child C stage"};
        setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);

        //Adapter creation
        SimpleExpandableListAdapter adapter =
                new SimpleExpandableListAdapter(
                        this,
                        parentList,
                        android.R.layout.simple_expandable_list_item_1,
                        new String[]{"parentKey"},
                        new int[]{android.R.id.text1},
                        childList,
                        android.R.layout.simple_expandable_list_item_2,
                        new String[]{"childKey1", "childKey2"},
                        new int[]{android.R.id.text1, android.R.id.text2}
                );

        ExpandableListView elv = findViewById(R.id.exListView);
        elv.setAdapter(adapter);

    }

    /**
     *Process to generate the contents to be displayed in the parent list
     * @param parentKey
     * @param value
     * @return parentList
     */
    private List<Map<String, String>> setParentList(String parentKey, String value) {
        Map<String, String> parentData = new HashMap<String, String>();
        parentData.put(parentKey, value);
        parentList.add(parentData);
        return parentList;
    }

    /**
     *Process to generate the contents to be displayed in the child list
     * @param childKey1
     * @param firstItemName
     * @param childKey2
     * @param secondItemName
     * @return childDataList
     */
    private List<Map<String, String>> setChildData(String childKey1, String firstItemName, String childKey2, String secondItemName) {
        childtData = new HashMap<String, String>();
        childtData.put(childKey1, firstItemName);
        childtData.put(childKey2, secondItemName);
        childDataList.add(childtData);
        return childDataList;
    }

    /**
     *Process to create as many child lists as displayed
     * @param childKey1
     * @param firstItemName
     * @param childKey2
     * @param secondItemName
     * @return childList
     */
    private List<List<Map<String, String>>> setChildDataList(String childKey1, String firstItemName[], String childKey2, String secondItemName[]) {
        childDataList = new ArrayList<>();
        //Repeat as many times as the number of items to create
        for (int i = 0; i < firstItemName.length; i++) {
            setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
        }
        childList.add(childDataList);
        return childList;
    }

}

Execution result

新しいビットマップ イメージ.jpeg

Commentary

Use the SimpleExpandableListAdapter to set the data you want to display as a list in the ExpandableListView. To store the data you want to display as a list in SimpleExpandableListAdapter, create it according to the following procedure.

procedure

  1. Process to generate the contents to be displayed in the parent list
  2. Process to generate the contents to be displayed in the child list
  3. Number of items in the child list, repeated processing
  4. Store the data to be displayed in the parent list in Map
  5. Store the data to be displayed in the child list in Map
  6. Store the parent list and child list in SimpleExpandableListAdapter

Storage of each data

Create as many parent lists as there are calls to the setParentList method. As an argument, pass the key and value to be stored in MAP.

setParentList.Storage of parent data


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parentList = new ArrayList<>();

     ~abridgement~

        //Generate what to display in the parent list
        setParentList("parentKey", "Parent 1");
        setParentList("parentKey", "Parent 2");

     ~abridgement~
    }

private List<Map<String, String>> setParentList(String parentKey, String value) {
        Map<String, String> parentData = new HashMap<String, String>();
        parentData.put(parentKey, value);
        parentList.add(parentData);
        return parentList;
    }

Create as many parent lists as there are calls to the setChildDataList method. As arguments, pass the key (1st and 3rd arguments) and value (2nd and 4th arguments) to be stored in MAP.

setChildDataList.Storage of child data


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     ~abridgement~
        
        childList = new ArrayList<>();

     ~abridgement~

        //Generate content to display in child list
        String firstItemName1[] = {"Child 1", "Child 2"};
        String secondItemName1[] = {"Child 1st stage", "Child 2nd stage"};
        setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);

        String firstItemName2[] = {"Child A", "Child B", "Child C"};
        String secondItemName2[] = {"Child A stage", "Child B stage", "Child C stage"};
        setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);

     ~abridgement~
     }

private List<Map<String, String>> setChildData(String childKey1, String firstItemName, String childKey2, String secondItemName) {
        childtData = new HashMap<String, String>();
        childtData.put(childKey1, firstItemName);
        childtData.put(childKey2, secondItemName);
        childDataList.add(childtData);
        return childDataList;
    }

    private List<List<Map<String, String>>> setChildDataList(String childKey1, String firstItemName[], String childKey2, String secondItemName[]) {
        childDataList = new ArrayList<>();
        //Repeat as many times as the number of items to create
        for (int i = 0; i < firstItemName.length; i++) {
            setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
        }
        childList.add(childDataList);
        return childList;
    }

Adapter storage

Store parent and child data in the adapter

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     ~abridgement~

        //Adapter creation
        SimpleExpandableListAdapter adapter =
                new SimpleExpandableListAdapter(
                        this,・ ・ ・ ①
                        parentList,・ ・ ・ ②
                        android.R.layout.simple_expandable_list_item_1,・ ・ ・ ③
                        new String[]{"parentKey"},・ ・ ・ ④
                        new int[]{android.R.id.text1},・ ・ ・ ⑤
                        childList,・ ・ ・ ②
                        android.R.layout.simple_expandable_list_item_2,・ ・ ・ ③
                        new String[]{"childKey1", "childKey2"},・ ・ ・ ④
                        new int[]{android.R.id.text1, android.R.id.text2}・ ・ ・ ⑤
                );

        ExpandableListView elv = findViewById(R.id.exListView);
        elv.setAdapter(adapter);

    }

① Context to which ExpandableListView is associated ②List ③ R value representing the layout of each line ④ MAP key ⑤ ID of TextView that displays characters in the layout

This is how to use ExpandableListView.

Recommended Posts

How to use ExpandableListView in Android Studio
How to use Lombok in Spring
How to use InjectorHolder in OpenAM
How to use classes in Java?
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to use Docker in VSCode DevContainer
How to use environment variables in RubyOnRails
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
Notes in Android studio
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
3 ways to import the library in Android Studio
[Rails] How to use select boxes in Ransack
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
[Ruby] How to use standard output in conditional branching
[Android Studio] How to change TextView to any font [Java]
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Z3 library in Scala with Eclipse
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
Understand how to use Swift's JSON Decoder in 3 minutes
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Android Studio] [Java] How to fix the screen vertically
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use Truth (assertion library for Java / Android)