[JAVA] How to change the process depending on the list pressed when there are multiple ListViews

Introduction

This is a method to change the processing depending on the touched list when the ListView has multiple lists as shown below.

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/food"
    android:id="@+id/food"></ListView>
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/drink"
    android:id="@+id/drink"></ListView>

code

When you touch the list of foods, "The food you chose is XX", When you touch the list of drinks, try to make a toast that displays "The drink you chose is XX".

strings.xml


<resources>
    <string name="app_name">ListView</string>
    <string name="text_food">Hood</string>
    <string name="text_drink">Drink</string>
    <string-array name="food">
        <item>meat</item>
        <item>fish</item>
    </string-array>
    <string-array name="drink">
        <item>juice</item>
        <item>tea</item>
    </string-array>
</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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_food"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/food"
        android:id="@+id/food"></ListView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_drink"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/drink"
        android:id="@+id/drink"></ListView>

</LinearLayout>

MainActivity.java


package com.websarva.wings.android.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        ListView foodListView = findViewById(R.id.food);
        ListView drinkListView = findViewById(R.id.drink);

        foodListView.setOnItemClickListener(new ListItemClickListener());
        drinkListView.setOnItemClickListener(new ListItemClickListener());
    }

    /**
     *Processing when item is pressed in ListView
     */
    private class ListItemClickListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            ListView foodList = findViewById(R.id.food);
            ListView drinkList = findViewById(R.id.drink);

            String item = (String) parent.getItemAtPosition(position);
            String show;

            if(parent == foodList){
                show = "The food you choose" + item;
                Toast.makeText(MainActivity.this,show,Toast.LENGTH_LONG).show();
            }

            if (parent == drinkList){
                show = "The drink you choose" + item;
                Toast.makeText(MainActivity.this,show,Toast.LENGTH_LONG).show();
            }
        }
    }
}

Execution result

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

Since the toast displayed on the left touched the meat, "The food you chose is XX" is displayed, The toast displayed on the right touched the juice, so "You chose it, so the drink is XX" Is displayed.

Commentary

 private class ListItemClickListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            ListView foodList = findViewById(R.id.food);・ ・ ・ ➀
            ListView drinkList = findViewById(R.id.drink);・ ・ ・ ➀

            String item = (String) parent.getItemAtPosition(position);
            String show;

            if(parent == foodList){・ ・ ・ ➁
                show = "The food you choose" + item;
                Toast.makeText(MainActivity.this,show,Toast.LENGTH_LONG).show();
            }

            if (parent == drinkList){・ ・ ・ ➁
                show = "The drink you choose" + item;
                Toast.makeText(MainActivity.this,show,Toast.LENGTH_LONG).show();
            }
        }
    }

➀ Get the ListView ID

Use the findViewById method to get the ID of each ListView to determine which list was touched.

➁ Change processing depending on the touched ListView

Compare the ID obtained in ➀ with parent, determine the touched ListView, and execute the process.

Recommended Posts

How to change the process depending on the list pressed when there are multiple ListViews
How to change the timezone on Ubuntu
Change the processing when the button on RecyclerView is pressed for each list
How to change the action with multiple submit buttons
[Ruby on Rails] How to change the column name
How to perform a specific process when the back button is pressed in Android Fragment
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to output the value when there is an array in the array
How to display products by category on the same list screen
[Rails 5] How to display the password change screen when using devise
[JPA] How to save when there are items other than keys in the intersection table related to ManyToMany
[Docker] How to build when the source code is bind-mounted on the container
Process to check if "some fields are duplicated" from the Bean List
Ransack sort_link How to change the color!
How to sort the List of SelectItem
[swift] How to control the behavior when the back button of NavigationBar is pressed
How to delete the database when recreating the application
How to install multiple JDKs on Ubuntu 18.04 LTS
http: // localhost: How to change the port number
[Java] Memo on how to write the source
[Rails / Uniqueness constraint] How to check model validation on the console / Uniqueness constraint for multiple columns
How to delete / update the list field of OneToMany
List how to learn from Docker to AKS on AWS
[Rails] How to change the column name of the table
Install MySQL 5.6 on CentOS6 [How to specify the version]
How to reduce the load on the program even a little when combining characters with JAVA
When you want to reflect the Master Branch information in the Current Branch you are currently working on