Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:Dialog Builder多重选择设置数组_Java_Android_Android Dialog - Fatal编程技术网

Java Android:Dialog Builder多重选择设置数组

Java Android:Dialog Builder多重选择设置数组,java,android,android-dialog,Java,Android,Android Dialog,我正在尝试跟随android文档中关于多个选择对话框的内容。我遇到了一个问题,我认为这与我试图加载的数组类型有关 public void addCondition(View view){ ArrayList<String> mHelperNames= new ArrayList<String>(); mHelperNames.add("Test Item"); mHelperNames.add("Test I

我正在尝试跟随android文档中关于多个选择对话框的内容。我遇到了一个问题,我认为这与我试图加载的数组类型有关

public void addCondition(View view){

         ArrayList<String> mHelperNames= new ArrayList<String>();
           mHelperNames.add("Test Item");
           mHelperNames.add("Test Item");
           mHelperNames.add("Test Item");


           mSelectedItems = new ArrayList();  

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("My Title")
                   .setMultiChoiceItems(mHelperNames, null,
                              new DialogInterface.OnMultiChoiceClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which,
                               boolean isChecked) {
                           if (isChecked) {

                               mSelectedItems.add(which);
                           } else if (mSelectedItems.contains(which)) {

                               mSelectedItems.remove(Integer.valueOf(which));
                           }
                       }
                   })

                   .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                            //Create onlcick method
                       }
                   })
                   .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                            //Create onlcick method
                       }
                   });
            builder.show();

    }
public void addCondition(视图){
ArrayList mHelperNames=新的ArrayList();
mHelperNames.添加(“试验项目”);
mHelperNames.添加(“试验项目”);
mHelperNames.添加(“试验项目”);
mSelectedItems=newarraylist();
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“我的标题”)
.setMultiChoiceItems(mHelperNames,null,
新的DialogInterface.OnMultiChoiceClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int,
布尔值(已检查){
如果(已检查){
mSelectedItems.add(哪个);
}else if(mSelectedItems.contains(which)){
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton(R.string.ok,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//创建onlcick方法
}
})
.setNegativeButton(R.string.cancel,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//创建onlcick方法
}
});
builder.show();
}
以上是我的代码,但它是eclipse中的红线城市:

在文档中,mSelectedItems从未声明过,我也不太确定我将它声明为什么

.setMultipleChiiceItems上的错误为:

类型AlertDialog.Builder中的方法setMultiChoiceItems(int,boolean[],DialogInterface.OnMultiChoiceClickListener)不适用于参数(ArrayList,null,new DialogInterface.OnMultiChoiceClickListener(){})

但如果我将其从字符串中更改,如何显示其中的文本项?任何帮助都将不胜感激


Tom

您必须为
设置多选项项
方法提供
CharSequence[]
,而不是
数组列表

您可以这样创建
mHelperNames

CharSequence[] mHelperNames = new CharSequence[] { "test item 1", "test item 2" };
不要忘记也声明
mSelectedItems

final List<Integer> mSelectedItems = new ArrayList<Integer>(); 

先生,欢迎您随时来我家吃饭。非常感谢。一个简单的问题,我如何在其他地方添加charsequence而不是在开始时添加静态列表?
    List<CharSequence> mHelperNames = new ArrayList<CharSequence>();
    mHelperNames.add("Test Item 1");
    mHelperNames.add("Test Item 2");

    final List<Integer> mSelectedItems = new ArrayList<Integer>();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("My Title")
            .setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), null,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which, boolean isChecked) {
                            if (isChecked) {

                                mSelectedItems.add(which);
                            } else if (mSelectedItems.contains(which)) {

                                mSelectedItems.remove(Integer
                                        .valueOf(which));
                            }
                        }
                    })