Reputation: 14278
For some reason, no matter what I change, I cannot get rid of the extra space after the last item in my ListView
. You can see it here under "item 3".
This happens even if I have a long list, and even if I pull down all the way.
I've tried every combination of match_parent
and wrap_content
in my layout with no success. Here's my 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>
Here's my 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();
}
Upvotes: 1
Views: 936
Reputation: 6351
The error is caused because your code instructs the builder to create two sets of data sources through calling
The same error will be caused if the code calls
together.
The solution is to just call setAdapter(), Do Not call setView() extra.
Upvotes: 0
Reputation: 344
AlertDialog.Builder setMultiChoiceItems method creates a list from the given array and supplies a listener to notify your app regarding changes. This method does not require setView to be called earlier, so by doing so, you are inflating an empty layout and adding it to the AlertDialog for no reason.
Although the documentation is a bit hazy on this point, I believe it, as you said in the comments, populates its own ListView with the CharSequence[] regardless of what view has been supplied to it via .setView() method.
Upvotes: 2