将item []传递给DialogFragment [英] Pass item[] to DialogFragment

查看:111
本文介绍了将item []传递给DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在活动运行时传递对话框片段的String []项,因为此String已更新,并且其显示DialogFragment的值也已更新,不能总是选择相同的值. 我已经阅读了以下主题: stackoverflow ,但我认为这并不是我真正需要的. 我知道有人帮我吗?

I am trying to pass the String [] items of my Dialog Fragment when the activity is running, as this String is updated and its values, which are showing the DialogFragment are updated and can not always choose the same. I have read this topic: stackoverflow but I think that's not exactly what I need. I know someone help me?

这是mi类DialogoSeleccion,它扩展了DialogFragment:

This is mi class DialogoSeleccion wich extends DialogFragment:

public class DialogoSeleccion extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final String[] items = {"Español", "Inglés", "Francés"};

        AlertDialog.Builder builder = 
                new AlertDialog.Builder(getActivity());

        builder.setTitle("Selección")
        .setMultiChoiceItems(items, null, 
                new DialogInterface.OnMultiChoiceClickListener() {
        public void onClick(DialogInterface dialog, int item, boolean isChecked) {
                Log.i("Dialogos", "Opción elegida: " + items[item]);
            }
        });

        return builder.create();
    }
}

这是主类的代码:

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
DialogoSeleccion dialogo = new DialogoSeleccion();
dialogo.show(fragmentManager, "tagSeleccion");

他无偿奉献的东西String[]可变的商品DIALOGlogoSeleccion和luego acceder desde el main de la forma:

He probado a poner String[] items como una variable de la clase DialogoSeleccion y luego acceder desde el main de la forma:

public class DialogoSeleccion extends DialogFragment {
    private String[] opciones;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {   
    ...

String[] opciones = {"1","2"}
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
DialogoSeleccion dialogo = new DialogoSeleccion();
dialogo.items= opciones[];
dialogo.show(fragmentManager, "tagSeleccion");

但这是行不通的.

感谢您的帮助

推荐答案

提交DialogoFragment时可以添加捆绑包

You can add a bundle when committing the DialogoFragment

Bundle bundle= new Bundle();
bundle.putStringArray(A_KEY,mArray);
DialogoSeleccion dialogo = new DialogoSeleccion();
dialogo.setArguments(bundle);

然后您在对话框中检索bundle参数

And then you retrieve the bundle arguments in your Dialog

String[] array = bundle.getArguments().getStringArray(A_KEY);

这篇关于将item []传递给DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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