Passing an Object to Fragment or DialogFragment upon Instantiation

前端 未结 4 2219
春和景丽
春和景丽 2021-02-07 15:47

I\'m trying to work out the correct way to pass in an Object to a Fragment or DialogFragment without breaking the \'empty constructor\' rule.

For example I have created

4条回答
  •  粉色の甜心
    2021-02-07 16:38

    I fixed my need with sharepreference. YuzdeDataUser is my Custom clas that I take it onClickItemListener from My Listview

    How to Send

      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");
    

    How to Receive

        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());
    
        }
    

提交回复
热议问题