AlertDialog在运行时决定了x个按钮

时间:2014-12-01 11:20:59

标签: android android-alertdialog

我正在尝试使用x个按钮生成警报diaglog。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Please select the correct AltCode");

                String btns = "";

                for(iPrepack i : prepackList)
                {
                btns = btns + "\"" + i.getAltcode() + "\",";
                }

                btns = btns.substring(0, btns.length() -1);

                builder.setItems(new CharSequence[]
                        {btns}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pp = prepackList.get(which);
                        populatescreen(pp);
                    }
                });
                builder.create().show();

我遇到的问题是我只用一个按钮显示所有我的altcodes,如何将它们分成单独的按钮?

1 个答案:

答案 0 :(得分:0)

如果有人在将来陷入困境

  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Please select the correct AltCode");


                List<String> acodes = new ArrayList<String>();
                for (iPrepack i : prepackList) {
                    acodes.add(i.getAltcode());
                }
                final CharSequence[] btns = acodes.toArray(new CharSequence[acodes.size()]);

                builder.setItems(btns, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pp = prepackList.get(which);
                        populatescreen(pp);
                    }
                });
                builder.create().show();