Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 实例化时将对象传递给Fragment或DialogFragment_Android_Android Fragments_Fragment_Android Dialogfragment - Fatal编程技术网

Android 实例化时将对象传递给Fragment或DialogFragment

Android 实例化时将对象传递给Fragment或DialogFragment,android,android-fragments,fragment,android-dialogfragment,Android,Android Fragments,Fragment,Android Dialogfragment,我正在尝试找到一种正确的方法,在不违反“”规则的情况下将对象传递给or 例如,我已经创建了一个自定义视图,对于我实例化的每个视图,我想关联一个DiagnogFragment。此对话框片段将用于显示控件,用户可以使用这些控件更改与其关联的自定义视图的某些方面。因为视图是一个我理解的对象,所以我不能使用它 我可以实现DialogFragment的newInstance(View)方法,即工厂模式,但如果系统保存了我的片段,然后在以后还原,会发生什么?就我所知,没有对视图对象的引用 有人能告诉我,我是

我正在尝试找到一种正确的方法,在不违反“”规则的情况下将对象传递给or

例如,我已经创建了一个自定义视图,对于我实例化的每个视图,我想关联一个DiagnogFragment。此对话框片段将用于显示控件,用户可以使用这些控件更改与其关联的自定义视图的某些方面。因为视图是一个我理解的对象,所以我不能使用它

我可以实现DialogFragment的newInstance(View)方法,即工厂模式,但如果系统保存了我的片段,然后在以后还原,会发生什么?就我所知,没有对视图对象的引用


有人能告诉我,我是否以错误的方式使用了片段,或者是否有办法将对象传递给片段,这也将处理系统稍后重建它的问题。

您可以将Bundle extra中的对象作为可打包对象()传递给Bundle,并在
onCreateView中传递给Bundle(Bundle savedInstanceState)
。如果用户翻转屏幕,则可以保存它们

编辑:包裹教程非常好

另一种方法是从您的ParentActivity获取数据对象,但我不确定这是否是一种好方法(但它是有效的…)

为此,您必须在活动中创建一个Getter

public YourObject getYourObject(){
   return mYourObecjt;
}

但是我想包裹是更好的方式,因为你可以重用你的片段而不需要任何依赖性…

你可以使用一个漂亮的JSON库,比如GSON或Genson来序列化对象-这是我对任何远程复杂对象的goto方法。GSON对于简单的操作稍微紧凑一些如果你的观点中有任何类型的继承/多态性,我强烈推荐Genson。你可以有一个带有参数的ctr,只要你记录它们


-在下载部分获取JAR。

对话框Fragmnet
类中,创建两个方法:

  • newInstance
    创建对话框片段的实例

  • setter来初始化对象

  • 并将
    setRetainInstance(true);
    添加到
    onCreate

    public class YourDialogFragment extends DialogFragment {
    
        ComplexVariable complexVar;
    
        public static YourDialogFragment newInstance(int arg, ComplexVariable complexVar) {
            YourDialogFragment frag = new MoveSongDialogFragment();
            Bundle args = new Bundle();
            args.putInt("count", arg);
            frag.setArguments(args);
            frag.setComplexVariable(complexVar);
            return frag;
        }
    
        public void setComplexVariable(ComplexVariable complexVar) {
            this.complexVar = complexVar;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }
    }
    
    然后,显示对话框

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    
    Fragment prev = manager.findFragmentByTag("yourTag");
    if (prev != null) {
        ft.remove(prev);
    }
    
    // Create and show the dialog.
    DialogFragment newFragment = YourFragmentDialog.newInstance(argument, yourComplexObject);
    newFragment.show(ft, "yourTag");
    

    我用sharepreference修复了我的需求。YuzdeDataUser是我的自定义clas,我从Listview的ClickItemListener中获取它

    如何发送

      YuzdeDataUser clickedObj = (YuzdeDataUser) parent.getItemAtPosition(position);
                //clickedObj.setTarihim();
                SharedPreferences shp = getSharedPreferences("sam", MODE_PRIVATE);
                SharedPreferences.Editor editor = shp.edit();
    
                Gson gson = new Gson();
                String json = gson.toJson(clickedObj);
                editor.putString("yoklamaList", json);
                editor.commit();
    
                FragmentManager fragmentManager = getSupportFragmentManager();
                AylikRaporPop userPopUp = new AylikRaporPop();
                userPopUp.show(fragmentManager, "sam");
    
    如何接收

        SharedPreferences shp = parent.getSharedPreferences("sam", MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
    
        Gson gson = new Gson();
        String json = shp.getString("yoklamaList", "");
        YuzdeDataUser user = gson.fromJson(json, YuzdeDataUser.class);
        if (user != null) {
            txt_ad.setText(user.getAd());
            txt_tur_tarih.setText(Util.getInstance(parent).writeNameOfTur(user.getTur_id()));
            txt_var.setText("" + user.getVar());
            txt_gorevli.setText("" + user.getGorevli());
            txt_yok.setText("" + user.getYok());
    
        }
    

    在片段中添加一个setter,然后在
    newfragment()之后调用该setter
    ?但您不想设置视图。您想告诉片段它需要显示什么,并且它应该能够自己生成视图。如果活动正在这样做,则可能意味着您没有正确地分割片段和活动。片段不会显示视图,它只是紧密关联但是片段需要将数据传递回视图(例如选择哪个单选按钮)正确的方法是创建一个接口,该接口由活动实现,片段可以调用方法。基本上,该活动应该协调片段与活动中其他视图之间的任何交互。是的,您让我意识到我正在以不应该的方式使用片段。我能做的是传入视图的id以及包含该视图的片段。需要时,我的DialogFragment可以调用包含活动来传递数据。不确定Parcelable是否是我需要的,但通过阅读链接,我意识到我可能没有明确回答我的问题。我需要一个实际对象的引用。Parcelable对象是一个对象的重建/克隆实际对象?我需要引用,这样我可以实时更改视图。谢谢你的回复。我意识到我只需要引用实际对象,所以任何形式的序列化都会使事情复杂化。我只需要通过活动进行沟通,这是一种不定的方式。不确定这是一种好方法,因为可以自动还原(例如,在旋转期间),然后yourVar值将为空。因此,DialogFragment在系统自动实例化时将被错误地呈现。似乎将序列化对象放在片段的参数中是唯一好的解决方案。或者使用getActivity()进行标准通信+接口方法。@goRGon如果您没有在fragment的onCreate setRetainInstance(true)中调用,则您是对的;更新了答案并添加了setRetainInstance(true);以供将来参考或在architecture components ViewModel中存储复杂变量
        SharedPreferences shp = parent.getSharedPreferences("sam", MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
    
        Gson gson = new Gson();
        String json = shp.getString("yoklamaList", "");
        YuzdeDataUser user = gson.fromJson(json, YuzdeDataUser.class);
        if (user != null) {
            txt_ad.setText(user.getAd());
            txt_tur_tarih.setText(Util.getInstance(parent).writeNameOfTur(user.getTur_id()));
            txt_var.setText("" + user.getVar());
            txt_gorevli.setText("" + user.getGorevli());
            txt_yok.setText("" + user.getYok());
    
        }