gpt4 book ai didi

java - 将 arrayList 传递给对话框中的 setMultiChoiceItems

转载 作者:行者123 更新时间:2023-11-29 19:06:21 24 4
gpt4 key购买 nike

我正在实现一个简单的对话框,其中包含一个选中的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com