There was no problem in selecting only one (Single Choice), but I was addicted to multiple selections, so I made a note.
activity_main.xml Layout that just displays the button.
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:gravity="center"
android:orientation="vertical"
tools:context="example.com.samplemultichoice.MainActivity">
<TextView
android:id="@+id/btnOpenDialog"
android:layout_width="200dp"
android:layout_height="60dp"
android:background="#000000"
android:textColor="#FFFFFF"
android:gravity="center"
android:text="Dialog open"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
choice.xml Layout for one case in the dialog
choice.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="match_parent"
android:layout_gravity="center" >
<LinearLayout
android:id="@+id/checkboxArea"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="20">
<ImageView
android:id="@+id/agentCheck"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:scaleType="fitCenter"
android:src="@drawable/check_off" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="80">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="32dp"
android:text="Dummy text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="20dp"
android:text="Dummy text" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java The final thing.
Direction such as. Actually, from here, I narrow down by name before displaying.
By the way, the test data uses Use the list of managers in Osaka City.
MainActivity.java
package example.com.samplemultichoice;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView btnOpenDialog; //Button to open Dialog with tap
private ArrayList<Item> allItems; //Storage location of option information
private Context con = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpenDialog = (TextView) findViewById(R.id.btnOpenDialog);
//Prepare choices
allItems = this.setData();
//Button click event
btnOpenDialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Prepare choices
DialogAdapter adapter = new DialogAdapter(getApplicationContext(), 0, allItems);
ListView listView = new ListView(con);
AlertDialog.Builder dialog = new AlertDialog.Builder(con);
//Prepare processing when tapping an option
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Switching check status
allItems.get(position).setFlg(!(allItems.get(position).isFlg()));
//Switch the display of the check image
ImageView checkImg = view.findViewById(R.id.agentCheck);
if (allItems.get(position).isFlg()) {
checkImg.setImageResource(R.drawable.check_on);
} else {
checkImg.setImageResource(R.drawable.check_off);
}
}
});
//Preparation of title
TextView newTitle = new TextView(con);
newTitle.setText("Please select");
newTitle.setTextSize(24.0f);
//Dialog display
listView.setAdapter(adapter);
dialog.setView(listView).create();
dialog.setPositiveButton("Done", null);
dialog.setCustomTitle(newTitle);
dialog.show();
}
});
}
//Original adapter
private class DialogAdapter extends ArrayAdapter<Item> {
private LayoutInflater inflater;
private DialogAdapter(Context context, int resource, List<Item> objects) {
super(context, resource, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
Item item = getItem(position);
if (null == v) {
v = inflater.inflate(R.layout.choice, null);
}
//Main display item
TextView textMain = v.findViewById(R.id.textMain);
textMain.setText(item.getName());
//Sub display item
TextView textSub = v.findViewById(R.id.textSub);
textSub.setText(item.getSub());
//Switch the check box image depending on the check status
ImageView checkImg = v.findViewById(R.id.agentCheck);
if (item.isFlg()) {
checkImg.setImageResource(R.drawable.check_on);
} else {
checkImg.setImageResource(R.drawable.check_off);
}
return v;
}
}
//Subclass for holding information displayed as an option
private class Item {
private int id = 0; //For system identification
private String name = ""; //Main display item
private String sub = ""; //Sub display item
private boolean flg = false; //Check status
private int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
private String getSub() {
return sub;
}
private void setSub(String sub) {
this.sub = sub;
}
private boolean isFlg() {
return flg;
}
private void setFlg(boolean flg) {
this.flg = flg;
}
}
//Create choice data for testing
private ArrayList<Item> setData() {
Item item;
ArrayList<Item> testData = new ArrayList<>();
item = new Item();
item.setId(1);item.setName("Kenji Hand");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(2);item.setName("Yukihiro Inoue");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(3);item.setName("Yoshihiro Tanaka");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(4);item.setName("Katsuhiro Mizumori");item.setSub("Deputy Capital Promotion Bureau General Affairs Department Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(5);item.setName("Kimihide Yoshimura");item.setSub("Deputy Capital Promotion Bureau General Affairs Department Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(6);item.setName("Gusukuma Masaki");item.setSub("Deputy Capital Promotion Bureau General Affairs Section Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(7);item.setName("Nishioka Daizo");item.setSub("Deputy Capital Promotion Bureau General Affairs Section Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(8);item.setName("Yoshikazu Matsui");item.setSub("Deputy Capital Promotion Bureau Deputy Capital Planning Promotion Department Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(9);item.setName("Hiroshi Sakata");item.setSub("Deputy Capital Promotion Bureau Deputy Capital Planning Promotion Department Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(10);item.setName("Yuji Kawaguchi");item.setSub("Deputy Capital Promotion Bureau Planning Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(11);item.setName("Hashimoto Shizuko");item.setSub("Deputy Capital Promotion Bureau Planning Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(12);item.setName("Okumura Kakuichi");item.setSub("Deputy Capital Promotion Bureau Business Restructuring Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(13);item.setName("Hironori Hashimoto");item.setSub("Deputy Capital Promotion Bureau Business Restructuring Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(14);item.setName("Tomohiro Inoue");item.setSub("Deputy Capital Promotion Bureau System Planning Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(15);item.setName("Fukuoka Hirotaka");item.setSub("Deputy Capital Promotion Bureau System Planning Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(16);item.setName("Tomohiro Enoshita");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(17);item.setName("Kenji Masuda");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(18);item.setName("Kobayashi Masumi");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(19);item.setName("Takeshi Awara");item.setSub("Deputy Capital Promotion Bureau Financial Coordination Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(20);item.setName("Masanobu Kusumi");item.setSub("Deputy Capital Promotion Bureau Financial Coordination Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(21);item.setName("Masaaki Kawada");item.setSub("Deputy Capital Promotion Bureau Asset and Debt Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(22);item.setName("Yoshiyuki Nakagawa");item.setSub("Deputy Capital Promotion Bureau Asset and Debt Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(23);item.setName("Shinzen Okada");item.setSub("Deputy Capital Promotion Bureau System Coordination Department Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(24);item.setName("Isshi Oshita");item.setSub("Deputy Capital Promotion Bureau System Coordination Department Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(25);item.setName("Kuroda alone");item.setSub("Deputy Capital Promotion Bureau Strategic Coordination Section Chief");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(26);item.setName("Hideaki Mizuno");item.setSub("Deputy Capital Promotion Bureau Strategic Coordination Section Chief");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(27);item.setName("Makoto Tsujimoto");item.setSub("Deputy Capital Promotion Bureau Office Business Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(28);item.setName("Mitsuru Saito");item.setSub("Deputy Capital Promotion Bureau Office Business Manager");item.setFlg(true);
testData.add(item);
item = new Item();
item.setId(29);item.setName("Yoshihiro Taniguchi");item.setSub("Deputy Capital Promotion Bureau Organizational System Manager");item.setFlg(false);
testData.add(item);
item = new Item();
item.setId(30);item.setName("Takashi Sekoguchi");item.setSub("Deputy Capital Promotion Bureau Organizational System Manager");item.setFlg(true);
testData.add(item);
return testData;
}
}
Recommended Posts