由于我的声誉,我不得不将此作为一个答案而不是评论。但是,AlertDialog是否包含列表视图?
标题
列表视图
取消
如果是这样的话,我会编辑这篇文章作为解决方案。你的代码很奇怪……所以我认为最好是从头开始。
用以下代码编辑
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.alert_listview, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) builderView.findViewById(R.id.alertTitle);
Button alertButton = (Button) builderView.findViewById(R.id.alertButton);
ListView alertListView = (ListView) builderView.findViewById(R.id.listView);
alertTitle.setText("YOU CAN SET THIS TO WHATEVER");
//Click the alert button from within the alert will dismiss the dialog box
alertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});
alert\u listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:text="Title"
android:id="@+id/alertTitle"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/alertTitle">
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="0dip"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@+id/alertButton"
android:text="BUTTON"/>
</LinearLayout>
</RelativeLayout>
这是对话框中的列表
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.alert_listview, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
List<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
arrayList.add("6");
arrayList.add("7");
arrayList.add("8");
arrayList.add("9");
arrayList.add("10");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, arrayList);
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) builderView.findViewById(R.id.alertTitle);
Button alertButton = (Button) builderView.findViewById(R.id.alertButton);
ListView alertListView = (ListView) builderView.findViewById(R.id.listView);
alertListView.setAdapter(arrayAdapter);
alertTitle.setText("YOU CAN SET THIS TO WHATEVER");
//Click the alert button from within the alert will dismiss the dialog box
alertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});