1

由於某種原因,無論我改變什麼,我都無法擺脫我的ListView中最後一項之後的額外空間。您可以在「項目3」下看到它。使用MultiChoiceItems的AlertDialog中的listview中的額外空間

ShortList

發生這種情況,即使我有一個長長的名單,即使我拉一路下跌。

LongList

我試着在我的佈局,但沒有成功的match_parentwrap_content每個組合。這裏是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    > 

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

這裏是我的AlertDialog

private void openMultiItemAlertDialog() { 
    ArrayList<String> selectedItems = new ArrayList<String>(); 
    ArrayList<CharSequence> itemsList = new ArrayList<CharSequence>(); 
    itemsList.add("item 1"); 
    itemsList.add("item 2"); 
    itemsList.add("item 3"); 
    itemsList.add("item 4"); 
    itemsList.add("item 5"); 
    itemsList.add("item 6"); 
    itemsList.add("item 7"); 
    itemsList.add("item 8"); 
    itemsList.add("item 9"); 

    final CharSequence[] finalItemsList = itemsList.toArray(
      new CharSequence[itemsList.size()]); 

    boolean[] booleanPrimativeArray = new boolean[finalItemsList.length]; 
    checkedItems = new boolean[finalItemsList.length]; 
    selectedItems.clear(); 


    final View view = inflater.inflate(R.layout.dialog_list, null); 
    AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle(R.string.dialog_title) 
     .setView(view) 
     .setCancelable(true) 
     .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       dialogMultiItem = null; 
      } 
     }) 
     .setMultiChoiceItems(finalItemsList, booleanPrimativeArray, new DialogInterface.OnMultiChoiceClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
       checkedItems[which] = isChecked; 
      } 
     }) 
     .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       for (int i = 0; i < checkedItems.length; i++) { 
        if (checkedItems[i]) { 
         selectedItems.add((String) finalItemsList[i]); 
        } 
       } 
       doStuffWithSelectedItems(selectedItems); 
       dialog.dismiss(); 
       dialogMultiItem = null; 
      } 
     }); 

    dialogMultiItem = dialog.show(); 
} 
+0

你設置高度爲「wrap_content」嘗試設置爲「fill_parent」 btw你在哪裏聲明你的按鈕在xml? – r4jiv007

+0

這是什麼.setView(view)在那裏做? – rhoadster91

+0

@ r4jiv007我試過了每個可能的「wrap_content」和「match_parent」的組合。儘管如此,這是一個很好的補充。我已經更新了這個問題。而不是迂腐,但它是API級別8的「match_parent」:)至於按鈕,我沒有用XML聲明它們。它們在AlertDialog構建器中設置。 – Patrick

回答

2

AlertDialog.Builder setMultiChoiceItems方法創建給定數組列表,並提供一個監聽器,通知你關於修改應用程序。這種方法不需要要求setView先被調用,所以通過這樣做,你正在給一個空的佈局充氣並無故將其添加到AlertDialog中。儘管文檔在這一點上有點模糊,但我相信它,正如你在評論中所說的那樣,不管通過.setView()方法向它提供了什麼視圖,它都使用CharSequence []填充它自己的ListView。 。

+0

那麼刪除額外空間的解決方案是什麼? – Byron

+0

@Byron看到rhoadster91對我的問題的評論。 – Patrick

0

因爲你的代碼通過調用

  • 的setView指示生成器來創建兩組數據源的錯誤引起的()
  • setMultiChoiceItems()。 在一起。如果代碼調用

    • 的setView()
    • setAdapter()

    一起

相同的錯誤將被引起的。

解決方法是隻調用setAdapter(),不要調用setView()。