Abm
Abm

Reputation: 311

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

Upvotes: 0

Views: 56

Answers (2)

Enowneb
Enowneb

Reputation: 1037

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

Upvotes: 2

MikeT
MikeT

Reputation: 57043

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.

Upvotes: 1

Related Questions