在对话片段选项选择更新片段 [英] Update fragment on dialog fragment option selection

查看:135
本文介绍了在对话片段选项选择更新片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有片段在组件上点击弹出窗口DialogFragment。此对话框片段保存的选项列表。当选择从列表中选择我想通知的片段,所以我可以运行领域的更新过程。 我做了这样的事情

I have fragment that on a component click pop-ups DialogFragment. This dialog fragment holds list of options. When an option from list is selected I want to notify fragment so I can run fields update procedure. I did something like this

@Override
public void onClick(DialogInterface dialog, int item) {
     updateSharedPreference(item);
     Log.e("ProfilePersonaListDialog", "Click on dialog, inside onClick");
     OnCloseListDialogListener act = (OnCloseListDialogListener) getActivity();
     act.onDialogListSelection();

     dismiss();
}

然而,这getActivity()上FragmentActivity,而不是触发对话片段,该片段调用。 我可以杀了当前打开/运行的片段,并呼吁将得到更新领域的一个新的实例,但是那是肮脏的解决方案,我将preFER避免的。

However this getActivity() calls on FragmentActivity and not the fragment that triggered the dialog fragment. I could kill currently open/running fragment and call a new instance that would get updated fields, but that is dirty solution that I would prefer to avoid.

任何建议如何去片段此更新一次对话片段中选择选项?

Any suggestions how to go about this update of fragment once option selected in dialog fragment?.

推荐答案

刚回来与解决方案。我的问题实际上是转发当前片段getTag()字符串作为秀()为DialogFragment参数。如果有人有兴趣在这里是工作的样本。

Just coming back with solution. My problem was actually forwarding current fragment getTag() string as parameter of show() for DialogFragment. If anyone interested here is working sample.

创建简单的监听器

public interface OnCloseListDialogListener {
    public void onDialogListSelection();
}

创建新的对话框,将延长DialogFragment

Create new dialog that will extend DialogFragment



public class ListDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {

    private PersonaData[] mPersonaData;
    private String[] mPersonaName;
    private final String TAG;

    public static ListDialogFragment newInstance(PersonaData[] personaData, String tag) {
        ListDialogFragment dialog = new ListDialogFragment(personaData, tag);
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    private ListDialogFragment(PersonaData[] personaData, String tag) {
        this.mPersonaData = personaData.clone();
        this.TAG = tag;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setCancelable(true);
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        setStyle(style, theme);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_title);
        mPersonaName = getData();//Your own implementation here
        builder.setNegativeButton("Cancel", this);
        builder.setSingleChoiceItems(mPersonaName, -1, new SingleChoiceListener());
        return builder.create();

    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
    }

    private class SingleChoiceListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            updateSharedPreference(item);
            OnCloseListDialogListener act = (OnCloseListDialogListener) getFragmentManager().findFragmentByTag(TAG);
            act.onDialogListSelection();
            dismiss();
        }
    }
}

然后从您希望把这种对话做的波纹管片段。 DIALOG只是字符串常量,我把那里只是对话框

And then in fragment from which you wish to call this dialog do as bellow. DIALOG is just String constant I put there just dialog


SOME_CLICKABLE.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        FragmentManager manager = getFragmentManager();
        ListDialogFragment dialog = ListDialogFragment.newInstance(mPersona, getTag());
        dialog.show(manager, DIALOG);
    }
});

这篇关于在对话片段选项选择更新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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