将 arrayList 传递给对话框中的 setMultiChoiceItems
Pass arrayList to setMultiChoiceItems in dialog
我正在实现一个简单的对话框,其中选中了 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
}
试试这个...如果有效请告诉我...
ArrayList<String> strBrandList = new ArrayList<String>();
for (int i = 0; i < brandList.size(); i++) {
strBrandList.add(brandList.get(i).getProductName())
}
我正在实现一个简单的对话框,其中选中了 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
}
试试这个...如果有效请告诉我...
ArrayList<String> strBrandList = new ArrayList<String>();
for (int i = 0; i < brandList.size(); i++) {
strBrandList.add(brandList.get(i).getProductName())
}