简体   繁体   English

android 在对话框中显示 arraylist 项

[英]android display arraylist items in dialog

I am having an arraylist fetching name and status of a person.我有一个 arraylist 获取一个人的姓名和状态。 Arraylist is storing the status and name. Arraylist 存储状态和名称。 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 :从代码中,您在unfiledrogspDoctorList中有一个ArrayList的目标显示String

// 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 .您可以考虑将ArrayListString转换为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来显示连接的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 ....代表问题中SQL的rest

See https://www.sqlite.org/lang_aggfunc.html#group_concat参见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.请注意,没有提供 GROUP BY 子句的GROUP是由所有提取的行组成的单个组(因此是 output 行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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