Alertdialog.Builder的setItems()onClick返回null对话框

时间:2014-06-11 14:17:59

标签: java android android-alertdialog

我正在制作一个自定义的Alertdialog.Builder,并冒昧地扩展了danoz73所做的工作,在https://github.com/danoz73/QustomDialog找到了。

我在类setItems()(AlertDialog.Builder的子类)中实现了我自己的QustomDialogBuilder.java方法。由于我传递的项目不是标准,因此该方法 NOT 标记为Override

    public QustomDialogBuilder setItems(ArrayList<KMColor> items, final DialogInterface.OnClickListener listener) {
    LinearLayout itemList = (LinearLayout) mDialogView.findViewById(R.id.items_list);//INFLATE THE LINEARLAYOUT INSIDE THE SCROLLVIEW
    Log.i("TAG","1 setItems items dialog is "+mDialog);//<--THIS IS NULL

    //HIDE UNUSED VIEWS SO THERE ISN'T A SPACE
    mDialogView.findViewById(R.id.customPanel).setVisibility(View.GONE);
    mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);

    for (int i = 0; i < items.size(); i++) {
        KMColor item = items.get(i);

        View listItem = inflateItem(item.name,item.color);
        listItem.setId(item.color);//VIEWS IDS CAN'T BE 0
        itemList.addView(listItem);
        Log.i("TAG","2 setItems items dialog is "+mDialog);//<--THIS IS NULL

        if (listener != null) {
            /*itemList <-- doesn't tell you what item was clicked*/
            listItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("TAG","3 setItems items dialog is "+mDialog);//<--STILL NULL 
                    listener.onClick(mDialog, v.getId());
                }
            });
        }
    }
    return this;
}

Custom Dialog with setItems()

这一切都很好我得到了我想要显示的自定义对话框。但是,当我选择一个项目时,我得到了id但是我从onClick重新调整的对话框为空,所以我无法忽略该对话框!

        alert.setItems(items,new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.i("TAG", "setItems dialog is " + dialog);//<--null, so I crash if I try to dismiss()

        }
    });

回到QustomDialogBuilder.java这里是我用来抓取对话框的show override。

    @Override
public AlertDialog show() {
    if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
    if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
    mDialog = super.show();//<--GET THE DIALOG TO PASS IN THE ONCLICK

    Log.i("TAG","AlertDialog show()  dialog is "+ mDialog);
    return mDialog;
}

我确信我可以以不同的方式获取对话框,但我真的很想理解为什么这不会像我期望的那样停止工作。我将不胜感激任何帮助。谢谢!

编辑以下是我构建对话框的方法

public void showColorPicker(){

    QustomDialogBuilder alert = new QustomDialogBuilder(context);
    alert.setTitle("Pick Color");
    alert.setTitleColorWithResource(getResources().getColor(R.color.white));
    alert.setDividerColorWithResource(getResources().getColor(R.color.white));


    ArrayList<KMColor> items = new ArrayList<KMColor>();
    items.add(new KMColor("Black",Color.BLACK));
    items.add(new KMColor("Blue",Color.BLUE));
    items.add(new KMColor("Cyan",Color.CYAN));
    items.add(new KMColor("Green",Color.GREEN));
    items.add(new KMColor("Dark Grey",Color.DKGRAY));
    items.add(new KMColor("Light Grey",Color.LTGRAY));
    items.add(new KMColor("Magenta",Color.MAGENTA));
    items.add(new KMColor("Red",Color.RED));
    items.add(new KMColor("White",Color.WHITE));
    items.add(new KMColor("Yellow",Color.YELLOW));

    alert.setItems(items,new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.i("TAG", "setItems dialog is " + dialog);//<--NULL

        }
    });
    alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            dialog.cancel();//<--WORKS FINE
        }
    });
    // create alert dialog
    AlertDialog alertDialog = alert.create();
    // show it
    alertDialog.show();
}

1 个答案:

答案 0 :(得分:0)

不好意思说我找到了答案。我的问题在这里

 // create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();

我在我的Alertdialog.Builder上调用了create()但是我Override create() QustomDialogBuilder.java Override show() alert.show(); {1}}因此我从来没有掌握对话!简单修复!上面的代码现在只是

Override

我现在也create() QustomDialogBuilder.java show()。与 @Override public AlertDialog create() { if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE); if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE); mDialog = super.create(); return mDialog; } 相同,但现在我不小心追了我的尾巴!

{{1}}