java - 安卓 : How to dismiss a dialog when an item is selected

标签 java android modal-dialog

我发现这个库实现了 Material 设计对话框,但逻辑在某个地方打败了我。我想在选择项目后关闭对话框。

适配器创建如下。

MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(new MaterialSimpleListAdapter.Callback() {
    @Override
    public void onMaterialListItemSelected(int i, MaterialSimpleListItem item) {
        long id     = item.getId();
        switch ((int) id){
            case 10 : Email();
        }
        ////I have to dismiss the dialog here, but its created below.
    }
});

添加项目

adapter.add(new MaterialSimpleListItem.Builder(this)
                .content("Send by Email")
                .icon(R.mipmap.ic_mail_gray_48dp)
                .backgroundColor(Color.WHITE)
                .iconPaddingDp(8)
                .id(10)
                .build());

然后从适配器创建对话框。

 MaterialDialog dialog = new MaterialDialog.Builder(this)
                .adapter(adapter, null)
                .autoDismiss(true)
                .show();

问题是 MaterialDialog 尚未在 onMaterialListItemSelected 中创建,因为它依赖于适配器?我该如何解决这个问题?

最佳答案

我建议进行以下更改:

//declare as global the dialog variable
private MaterialDialog dialog;

final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(new MaterialSimpleListAdapter.Callback() {
            @Override
            public void onMaterialListItemSelected(int index, MaterialSimpleListItem item) {
                dialog.dismiss();
            }
        });

        //remove the autoDismiss(true) option, better use the Activity
        //context
        dialog = new MaterialDialog.Builder(MyActivity.this)
                .adapter(adapter, null)
                .show();

希望对你有帮助!!!

关于java - 安卓 : How to dismiss a dialog when an item is selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39221991/

相关文章:

java - 'createVideoSource(boolean )' in ' org.webrtc.PeerConnectionFactory' 不能应用于 '(org.webrtc.CameraVideoCapturer)'

java - 如何读取文本文件并存储在类 vector 中 - Android

java - (Java)尽管链接在浏览器中工作得很好,但 HTTP GET 请求不断收到 400 响应代码

java - 为什么 Tomcat 8.5 重新加载上下文

java - 使用 BufferedWriter/Reader 添加行数

android - 在网站上存储数据和从网站访问数据

iphone - 从模态视图 Controller 更改选项卡栏应用程序的选定索引

angular - 关闭后启动对话框未打开

javascript - 如何让 AlloyUI 模态主体在初始化为可见时正确渲染 : false?

java - 保留 Android 进度条或恢复它