我正在实现一个简单的对话框,其中包含选中的listview
。这是我到目前为止所做的:
CharSequence[] items = {"Brand A", "Brand B", "Brand C"};
AlertDialog.Builder builder = new AlertDialog.Builder(StrengthOfDemandsView.this);
builder.setTitle("Select Brands");
final ArrayList seletedItems=new ArrayList();
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
// indexSelected contains the index of item (of which checkbox checked)
@Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
seletedItems.add(indexSelected);
} else{
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
问题:
最初,我将一个数组传递给 setMultiChoiceItems
方法,它工作正常,但如何传递 ArrayList
而不是数组?像这样:
ArrayList<Products> brandList = new ArrayList<>();
每当我尝试将 ArrayList
传递给 setMultiChoiceItems
方法时,都会出现以下错误:
Cannot resolve method 'setMultiChoiceItems(java.util.ArrayList<com.application.marketvisit.dataItem.Products>, null, anonymous android.content.DialogInterface.OnMultiChoiceClickListener)'
最佳答案
您需要将String
数组传递给AlertDialog.Builder#setMultiChoiceItems
因此将其收集为字符串数组
String arr = new String[brandList.size()];
for(int i=0 ; i< brandList.size();i++){
arr[i] = brandList.get(i).getProductName();
//getProductName or any suitable method
}
关于java - 将 arrayList 传递给对话框中的 setMultiChoiceItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47089167/