Search code examples
android-sqliteandroid-alertdialog

android display arraylist items in dialog


I am having an arraylist fetching name and status of a person. Arraylist is storing the status and name. Its displaying one name at a time. How can I be able to display multiple names at once in alert dialog?

private ArrayList getunfiledRogspDoctorList() {
    SqlDataStore sd = new SqlDataStore(this);
    sd.open();
    String gspQuery = " SELECT * FROM "+ TABLE_DOCTOR + " WHERE " + Queryclass.DOCTOR_ROGSP_STATUS + " == " + 0 + " AND " + Queryclass.DOCTOR_DATE_ID + " = '" + selectionID + "'";
    Cursor gspCu = sd.getData(gspQuery);
    if(gspCu.moveToFirst()){
        do {
            rogspname = gspCu.getString(gspCu.getColumnIndex(Queryclass.DOCTOR_CONTACTNAME));
            unfiledrogspDoctorList.add(gspCu.getString(gspCu.getColumnIndex(Queryclass.DOCTOR_ROGSP_STATUS)) + rogspname);
        }while (gspCu.moveToNext());

    }
    gspCu.close();
    sd.close();
    System.out.println("unfiledrogspDoctorList "+unfiledrogspDoctorList);

    return unfiledrogspDoctorList;
}

Solution

  • From the code, you are having an ArrayList of your target display String in unfiledrogspDoctorList:

    // Suggest to also define the type of your returning ArrayList
    private ArrayList<String> getunfiledRogspDoctorList() {
        // Define a local ArrayList
        ArrayList<String> unfiledrogspDoctorList = new ArrayList<>();
        SqlDataStore sd = new SqlDataStore(this);
        sd.open();
        String gspQuery = " SELECT * FROM "+ TABLE_DOCTOR + " WHERE " + Queryclass.DOCTOR_ROGSP_STATUS + " == " + 0 + " AND " + Queryclass.DOCTOR_DATE_ID + " = '" + selectionID + "'";
        Cursor gspCu = sd.getData(gspQuery);
        if(gspCu.moveToFirst()){
            do {
                rogspname = gspCu.getString(gspCu.getColumnIndex(Queryclass.DOCTOR_CONTACTNAME));
                unfiledrogspDoctorList.add(gspCu.getString(gspCu.getColumnIndex(Queryclass.DOCTOR_ROGSP_STATUS)) + rogspname);
            }while (gspCu.moveToNext());
    
        }
        gspCu.close();
        sd.close();
        System.out.println("unfiledrogspDoctorList "+unfiledrogspDoctorList);
    
        return unfiledrogspDoctorList;
    }
    

    You can consider to convert your ArrayList of String into just a String.

    private String concat(ArrayList<String> unfiledrogspDoctorList) {
      StringBuilder sb = new StringBuilder();
      for (String item : unfiledrogspDoctorList) {
        sb.append(item);
        sb.append(","); // Or change into other separate you would like to display
      }
      sb.setLength(Math.max(0, sb.length() - 1)); // Remove the appending character
      return sb.toString();
    }
    

    Then you can make use of an AlertDialog to display that concatenated String.

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
      .setMessage(concat(getunfiledRogspDoctorList()))
      .setCancelable(false)
      .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          // Do anything upon pressing OK button
        }
       );
    AlertDialog alert = builder.create();
    alert.show();