Как я могу отобразить список выбранных флажков в диалоговом окне предупреждения? - PullRequest
0 голосов
/ 28 января 2020

Я пытаюсь правильно отобразить список выбранных флажков, сохраненных в массиве, и отобразить их в виде списка в диалоговом окне предупреждения.

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();
 }

Я могу получить курсы, но они отображают и объект массива. я хочу отобразить курсы в виде списка флажков в диалоговом окне предупреждения.

1 Ответ

0 голосов
/ 28 января 2020

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 вместо показа на разных индексах. Таким образом, вместо использования вышеуказанной строки, используйте для l oop для вставки значений в массив

Отредактировано для FOR L oop Просто пример

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

Попробуйте что-то вроде этого

...