无法获取DialogFragment以编程方式解雇 [英] Cannot get DialogFragment to dismiss programatically

查看:123
本文介绍了无法获取DialogFragment以编程方式解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个展示的项目列表的DialogFragment从(类似于在信息的连接对话框)挑。

我的问题是我不能当选择一个项目以关闭该对话框。我已经打过电话辞退() getDialog()。解雇()中的 OnItemClickListener ,没有运气。我试图通过FragmentManager删除的对话框中,我已经试过 fragmentManager.popBackStack(),都无济于事。我不能让这个对话框驳回。当点击对话框外或点击返回按钮,但没有在我的code将使它消失就消失罚款。

有没有人见过这个?如何获取该对话框正确解雇?

对话框code:

 公共类ShareDialog扩展DialogFragment {公共枚举ShareType {
    相机,图库,网站,白板,浏览,
}BaseAdapter mShareAdapter =新BaseAdapter(){    @覆盖
    公共查看getView(INT位置,查看内容查看,ViewGroup中父){
        TextView的视图= NULL;
        如果(内容查看== NULL){
            鉴于=(TextView中)getLayoutInflater(空).inflate(android.R.layout.simple_list_item_1,父母,假);
        }其他{
            鉴于=(TextView的)内容查看;
        }
        INT拉伸= 0;
        开关(ShareType.values​​()[位置]){
        案例浏览:
            view.setText(浏览内容......);
            画= R.drawable.ic_share_browse;
            打破;
        案例摄像头:
            view.setText(图像从相机......);
            画= R.drawable.ic_share_camera;
            打破;
        案例库:
            view.setText(从库图片...);
            画= R.drawable.ic_share_gallery;
            打破;
        案例网址:
            view.setText(新浏览会话);
            画= R.drawable.ic_share_web;
            打破;
        案例白板:
            view.setText(新白板);
            画= R.drawable.ic_share_whiteboard;
            打破;
        }
        view.setCompoundDrawablesWithIntrinsicBounds(拉伸,0,0,0);
        view.setCompoundDrawablePadding(8);
        返回视图。
    }    @覆盖
    众长getItemId(INT位置){
        返回ShareType.values​​()[位置] .ordinal();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回ShareType.values​​()[位置]
    }    @覆盖
    公众诠释的getCount(){
        返回ShareType.values​​()的长度。
    }
};公共对话框onCreateDialog(android.os.Bundle savedInstanceState){
    AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
    builder.setTitle(「股份哪个?);
    ListView控件列表=新的ListView(getActivity());
    list.setAdapter(mShareAdapter);
    list.setOnItemClickListener(新AdapterView.OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的itemId){
            解雇(); // WHY NOT这会起作用?
            如果(听众!= NULL)
                listener.newShare((ShareType)mShareAdapter.getItem(位置));
        }
    });
    builder.setView(名单);
    返回builder.create();
}公共接口ShareDialogListener {
    无效newShare(ShareType型);
}私人ShareDialogListener侦听器;@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    //验证主机活动实现回调接口
    尝试{
        //实例化AutoconnectListener,所以我们可以将事件发送到主机
        监听=(ShareDialogListener)活性;
    }赶上(抛出ClassCastException E){
        //活动不实现接口,抛出异常
        抛出新ClassCastException异常(activity.toString()+必须实现ShareDialogListener);
    }
}}


解决方案

对于一些 - 未知的我 - 的原因,你从getDialog()返回对话框引用不是你想要在监听器里,当工作之一。您需要在对话框的引用)所提供,当你调用builder.create(;

例如:

 最后AlertDialog对话框= builder.create();
    list.setOnItemClickListener(新AdapterView.OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            dialog.dismiss();
        }
    });
    返回对话框;

I have a DialogFragment that shows a list of items to pick from (similar to the attach dialog in Messaging).

My problem is that I cannot get this dialog to dismiss when an item is selected. I've tried calling dismiss() and getDialog().dismiss() inside the OnItemClickListener, no luck. I've tried to remove the dialog through the FragmentManager, I've tried fragmentManager.popBackStack(), all to no avail. I cannot get this dialog to dismiss. It goes away fine when clicking outside the dialog or hitting the back button, but nothing in my code will make it go away.

Has anyone seen this before? How do I get the dialog to dismiss correctly?

Dialog Code:

public class ShareDialog extends DialogFragment {

public enum ShareType {
    Camera, Gallery, Web, Whiteboard, Browse,
}

BaseAdapter mShareAdapter = new BaseAdapter() {

    @Override
    public View getView(int position, View contentView, ViewGroup parent) {
        TextView view = null;
        if (contentView == null) {
            view = (TextView) getLayoutInflater(null).inflate(android.R.layout.simple_list_item_1, parent, false);
        } else {
            view = (TextView) contentView;
        }
        int draw = 0;
        switch (ShareType.values()[position]) {
        case Browse:
            view.setText("Browse Content...");
            draw = R.drawable.ic_share_browse;
            break;
        case Camera:
            view.setText("Image from Camera...");
            draw = R.drawable.ic_share_camera;
            break;
        case Gallery:
            view.setText("Image from Gallery...");
            draw = R.drawable.ic_share_gallery;
            break;
        case Web:
            view.setText("New Browsing Session");
            draw = R.drawable.ic_share_web;
            break;
        case Whiteboard:
            view.setText("New Whiteboard");
            draw = R.drawable.ic_share_whiteboard;
            break;
        }
        view.setCompoundDrawablesWithIntrinsicBounds(draw, 0, 0, 0);
        view.setCompoundDrawablePadding(8);
        return view;
    }

    @Override
    public long getItemId(int position) {
        return ShareType.values()[position].ordinal();
    }

    @Override
    public Object getItem(int position) {
        return ShareType.values()[position];
    }

    @Override
    public int getCount() {
        return ShareType.values().length;
    }
};

public Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Share which?");
    ListView list = new ListView(getActivity());
    list.setAdapter(mShareAdapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long itemId) {
            dismiss(); // WHY DOESN'T THIS WORK???
            if (listener != null)
                listener.newShare((ShareType) mShareAdapter.getItem(position));
        }
    });
    builder.setView(list);
    return builder.create();
}

public interface ShareDialogListener {
    void newShare(ShareType type);
}

private ShareDialogListener listener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the AutoconnectListener so we can send events to the host
        listener = (ShareDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString() + " must implement ShareDialogListener");
    }
}

}

解决方案

For some - unknown to me - reason, the dialog reference you get back from getDialog() isn't the one you want to work with when inside the listener. You need a reference to the dialog as provided you when you call builder.create();

For example:

    final AlertDialog dialog = builder.create();
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dialog.dismiss();
        }
    });
    return dialog;

这篇关于无法获取DialogFragment以编程方式解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆