如何将数组列表传递给警报对话框的方法builder.setItems

时间:2018-10-23 14:29:39

标签: android

有什么方法可以将arrayList传递给alertDialog方法builder.setItems,因为它正在接受字符串数组

public void getMember() {
    ArrayList<String> MemberList = new ArrayList<>();
    MemberList.add("tahir");
    MemberList.add("usman");
    MemberList.add("waqas");
    AlertDialog.Builder builder = new AlertDialog.Builder(ManagerHome.this);
    builder.setTitle("Member List");
    builder.setIcon(R.drawable.ic_format_list_bulleted_black_24dp);

    builder.setItems(MemberList, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将MemberList转换为数组,然后将其传递给setItems方法。如下所示:

public void getMember() {
    ArrayList<String> MemberList = new ArrayList<>();
    MemberList.add("tahir");
    MemberList.add("usman");
    MemberList.add("waqas");
    AlertDialog.Builder builder = new AlertDialog.Builder(ManagerHome.this);
    builder.setTitle("Member List");
    builder.setIcon(R.drawable.ic_format_list_bulleted_black_24dp);

    builder.setItems(MemberList.toArray(new String[0]), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
}

enter image description here