2011-06-21 8 views
11

ListViewとメッセージの両方でダイアログを作成する必要がありますが、http://code.google.com/p/android/issues/detail?id=10948では標準のAlertDialogではできません。だから、私はテキストとリストビューでカスタムビューを作成し、それをダイアログに添付することに決めました。リストビューとメッセージを含むダイアログ

ただし、私のリストビューは空のままです。また

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

私が持っている:すべてのヘルプは大歓迎ですdialog_with_empty_list_view

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

は結果である:ここ

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

とは、ダイアログのレイアウトである。ここのJavaコードであります。ありがとう!

答えて

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

セットの向きのようなレイアウト

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

垂直であるだろう

関連する問題