簡體   English   中英

如何在警報對話框中顯示選定復選框的列表?

[英]how can i display a list of selected checkboxes in an alert dialog?

我正在嘗試正確顯示存儲在數組中的選定復選框列表,並將它們顯示為警報對話框中的列表。

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

我可以獲得課程,但它們會顯示和一個數組對象。 我想在警報對話框中將課程顯示為復選框列表。

在此處輸入圖片說明 我已經像這樣檢查了你的代碼

   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 索引處的所有字符串數據,而不是顯示在不同的索引上。 因此,不要使用上面的行使用 for 循環在數組中插入值

為 FOR 循環編輯只是一個例子

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

嘗試這樣的事情

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM