4

我有一个 DialogFragment 显示要从中选择的项目列表(类似于消息传递中的附加对话框)。

我的问题是我无法让这个对话框选择选择项目时。我试过打电话dismiss()getDialog().dismiss()里面OnItemClickListener,没有运气。我试图通过 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, 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");
    }
}

}
4

3 回答 3

3

为什么不使用 AlertDialog.Builder 上可用的方法来构建列表,而不是创建自己的 ListView 并填充它?

我已经修改了您的示例代码以显示它是如何工作的,并且在此示例中,对话框dismiss() 功能正常。

public Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setSingleChoiceItems(mShareAdapter, 0, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (listener != null) {
                    listener.newShare((ShareType) mShareAdapter.getItem(which));
                }
            }
        });
        builder.setTitle("Share which?");
        return builder.create();
    }
于 2013-02-06T21:50:12.967 回答
2

对于某些 - 我不知道 - 原因,您从 getDialog() 返回的对话框引用不是您想要在侦听器内部使用的引用。您需要调用 builder.create(); 时提供的对对话框的引用;

例如:

    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;
于 2013-06-14T17:26:12.853 回答
2

哈....

我已经找到了...

这实际上是我们的原因......我们试图膨胀一个xml,并调用:

DialogFragment.this.getLayoutInflater(null).inflate(...);

就像我在评论中所说的那样,这个调用会导致创建 4 个对话框,然后一切都搞砸了。

正确的方法是调用:

LayoutInflater layoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(...);

这个修复一开始就为我解决了烦人的错误!

于 2013-08-12T12:35:25.203 回答