我做了这样的事
@OverrIDepublic 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而不是调用触发对话框片段的片段.
我可以杀死当前打开/运行的片段并调用一个可以获得更新字段的新实例,但这是我希望避免的脏解决方案.
有什么建议如何在对话框片段中选择片段一次更新选项?
解决方法 刚刚回来解决方案.我的问题实际上是转发当前片段getTag()字符串作为DialogFragment的show()参数.如果有兴趣在这里工作样品.创建简单的侦听器
public interface OnCloseListDialogListener { public voID onDialogListSelection();}
创建将扩展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只是String常量我把它放在那里只是对话框
SOME_CliCKABLE.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { FragmentManager manager = getFragmentManager(); ListDialogFragment dialog = ListDialogFragment.newInstance(mPersona,getTag()); dialog.show(manager,DIALOG); }});总结
以上是内存溢出为你收集整理的android – 更新对话框片段选项选择的片段全部内容,希望文章能够帮你解决android – 更新对话框片段选项选择的片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)