开发者

Dialog with list view and message

开发者 https://www.devze.com 2023-03-14 14:03 出处:网络
I need to create dialog with b开发者_高级运维oth ListView and message, however according to http://code.google.com/p/android/issues/detail?id=10948 it is not possible with standard AlertDialog. So I\'

I need to create dialog with b开发者_高级运维oth ListView and message, however according to http://code.google.com/p/android/issues/detail?id=10948 it is not possible with standard AlertDialog. So I've decided to create custom view with text and listview, and attach it to dialog.

However, my list view is drawn empty. Here is java code:

    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();

Also I have:

    final String[] ITEMS = new String[] { "a", "b", "c" };

and here is dialog layout:

<?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>

Here is result:

Dialog with list view and message

Any help is greatly appreciated. Thanks!


You are missing android:orientation="vertical" in linearlayout.

Your xml will be

<?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>


set orientation is vertical like

      <?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>

in your layout

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号