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>
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();
}
}
}
}
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.
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();
}
}
}
Use the findViewById method to get the ID of each ListView to determine which list was touched.
Compare the ID obtained in ➀ with parent, determine the touched ListView, and execute the process.
Recommended Posts