传递给AlertDialog.Buillder.setAdapter函数

问题描述

我正在通过继承ListPreference来实现我的特定首选项。

onPrepareDialogBuilder回调中,我创建适配器,并使用DialogInterface.onClickListener将其传递给对话框生成器。

@Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {

        selectedindex = findindexOfValue(getValue());

        builder.setAdapter(mAdapter,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface,int i) {
                Logger.info(TAG,"please get called...");
            }
        });

        super.onPrepareDialogBuilder(builder);
    }

但是,当我从适配器填充的ListView中选择一个项目时,将不会调用此侦听器,并且该对话框将关闭,不会引发任何异常,甚至保存所选的首选项。

我实际上找到了解决此问题的方法,但是上面的示例应该可以工作。

调用AlertDialog.Builder.create时将调用此回调,因此ListView存在。

@Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        AlertDialog alertDialog = (AlertDialog) getDialog();
        ListView listView = alertDialog.getListView();
        listView.setonItemClickListener((adapterView,view,i,l) -> {
            Logger.info(TAG,"this is getting called nicely");
        });
    }

这使我很难过,因为这正是AlertDialog应该做的。当create调用DialogPreference函数时,参数将应用于Dialog实例。这个apply函数完成了我在这里所做的... (此片段来自AOSP)

// mOnClickListener is the callback passed to the setAdapter function.
if (mOnClickListener != null) {
                listView.setonItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent,View v,int position,long id) {
                        // my callback should be called here.
                        mOnClickListener.onClick(dialog.mDialogInterface,position);
                        // And since the dialog closes I assume this part is getting called aswell.   
                        if (!mIsSingleChoice) {

                            dialog.mDialogInterface.dismiss();
                        }
                    }
                });
            } 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)