我如何在警报对话框中显示所选复选框的列表?

问题描述 投票:-2回答:1

试图正确显示存储在阵列中的选定复选框的列表,并在警报对话框中将它们显示为列表。

if(registeredCourseList.getcoursessize() <= 5){
    System.out.println(registeredCourseList.getCoursesArray());

    // setup the alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Are you sure you want to register the following courses? ");

    // add a checkbox list
    String[] courses ={String.valueOf(registeredCourseList.getCourses().toString())};
    boolean[] checkedItems = {true, true, true, true, true};
    builder.setMultiChoiceItems(courses, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which, boolean isChecked) {
             // user checked or unchecked a box
         }
    });

    // add OK and Cancel buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
              // user clicked OK
              // send the map or array to the server
              try {
                   processRegistration(mView,registeredCourseList.getCoursesArray());
              } catch (UnsupportedEncodingException e) {
                   e.printStackTrace();
              }

              Context context = getApplicationContext();
              CharSequence text = "Registered Courses Successfully";
              int duration = Toast.LENGTH_SHORT;

              Toast toast = Toast.makeText(context, text, duration);
              toast.show();

              onBackPressed();
          }
      });
      builder.setNegativeButton("Cancel", null);

      // create and show the alert dialog
      AlertDialog dialog = builder.create();
      dialog.show();

 } else {

       Context context = getApplicationContext();
       CharSequence text = "Choose at least 5 courses";
       int duration = Toast.LENGTH_SHORT;

       Toast toast = Toast.makeText(context, text, duration);
       toast.show();

       //back to the reg activity
       // onBackPressed();
 }

我可以获得课程,但显示这些课程和一个数组对象。我想在警报对话框中将课程显示为复选框列表。

java android arrays android-alertdialog
1个回答
0
投票

enter image description here我已经像这样检查过您的代码

   AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Are you sure you want to register the following courses? ");

    // add a checkbox list
    String[] courses ={"acx","sadsa","ksk","ksj","ywtgd"};
    boolean[] checkedItems = {true, true, true, true, true};
    builder.setMultiChoiceItems(courses, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            // user checked or unchecked a box
        }
    });
    builder.show();

并且显示五个不同的复选框。检查您的String[] courses ={String.valueOf(registeredCourseList.getCourses().toString())};我猜这行的输出都是0索引处的所有String数据,而不是显示在不同的索引处。因此,而不是使用上面的行用于循环在数组内插入值

为FOR循环编辑只是一个例子

  ArrayList courses=new ArrayList();
    for(int i=0;i<registeredCourseList.size;i++){
        courses.add(registeredCourseList.get(i).getCourses());
    }

尝试这样的事情

© www.soinside.com 2019 - 2024. All rights reserved.