简体   繁体   中英

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

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

You could use:-

SELECT group_concat(name) FROM ....

or to place each on a line you could change the default comma separator to a line feed using

SELECT group_concat(name,'\n') FROM ....
  • .... representing the rest of the SQL in the question

See https://www.sqlite.org/lang_aggfunc.html#group_concat

  • note that the GROUP as no GROUP BY clause is provided is a single group (and therefore output row) made up of ALL extracted rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM