带列表视图和消息的对话框

lst*_*kov 11 android listview dialog android-alertdialog

我需要创建包含ListView和消息的对话框,但是根据http://code.google.com/p/android/issues/detail?id=10948,使用标准AlertDialog是不可能的.所以我决定使用text和listview创建自定义视图,并将其附加到对话框.

但是,我的列表视图是空的.这是java代码:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Hello, title!");

    LayoutInflater factory = LayoutInflater.from(this);
    View content = factory.inflate(R.layout.dialog, null);

    ListView lv = (ListView) content.findViewById(R.id.list);
    lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice, ITEMS));
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    builder.setView(content).setPositiveButton("OK", this).setNegativeButton("Cancel", this);

    AlertDialog alert = builder.create();
    alert.show();
Run Code Online (Sandbox Code Playgroud)

我也有:

    final String[] ITEMS = new String[] { "a", "b", "c" };
Run Code Online (Sandbox Code Playgroud)

这是对话框布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
    ></ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

结果如下: dialog_with_empty_list_view

任何帮助是极大的赞赏.谢谢!

Sun*_*hoo 4

您缺少android:orientation="vertical"线性布局。

你的 xml 将是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"

    ></ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)