android – 无法以编程方式解除DialogFragment

android – 无法以编程方式解除DialogFragment,第1张

概述我有一个DialogFragment,显示了要从中选择的项目列表(类似于Messaging中的附加对话框). 我的问题是,当选择一个项目时,我无法关闭此对话框.我试过在OnItemClickListener中调用dismiss()和getDialog().dismiss(),没有运气.我试图通过FragmentManager删除对话框,我尝试过fragmentManager.popBackStac 我有一个DialogFragment,显示了要从中选择的项目列表(类似于Messaging中的附加对话框).

我的问题是,当选择一个项目时,我无法关闭此对话框.我试过在OnItemClickListener中调用dismiss()和getDialog().dismiss(),没有运气.我试图通过FragmentManager删除对话框,我尝试过fragmentManager.popBackStack(),但都无济于事.我不能让这个对话消失.单击对话框外部或按下后退按钮时,它会消失,但我的代码中的任何内容都不会消失.

谁看过这个吗?如何让对话框正确解除

对话代码:

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);        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;@OverrIDepublic 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");    }}}
解决方法 对于某些人 – 我不知道 – 的原因,您从getDialog()返回的对话框引用不是您在侦听器内部时要使用的对象引用.当您调用builder.create()时,您需要提供对话框的引用.

例如:

final AlertDialog dialog = builder.create();    List.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        @OverrIDe        public voID onItemClick(AdapterVIEw<?> parent,long ID) {            dialog.dismiss();        }    });    return dialog;
总结

以上是内存溢出为你收集整理的android – 无法以编程方式解除DialogFragment全部内容,希望文章能够帮你解决android – 无法以编程方式解除DialogFragment所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129858.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存