对话框片段包不传递数据

时间:2017-12-13 13:46:22

标签: android android-fragments android-bundle

我正在打开一个dialogfragment并捆绑数据。我不知道为什么,但捆绑的数据似乎没有过去。我在这里寻找解决方案,但我看不出我做错了什么:

这是我打开片段并附加捆绑数据的方式:

FragmentManager fm = ((MainActivity)context).getSupportFragmentManager();
InfoFragment infoFragment = new InfoFragment();
Bundle bundle = new Bundle();
bundle.putString("TITLE", reminder.getReminderTitle());
bundle.putString("DESCRIPTION", reminder.getReminderDescription());
infoFragment.setArguments(bundle);

// Show DialogFragment
infoFragment.show(fm, "InfoFragment");

在片段中,我正在检索捆绑的数据,如下所示:\

public class InfoFragment extends DialogFragment implements View.OnClickListener{

   TextView titleTextView;
   TextView descriptionTextView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_info, container,false);

        // Iniate ui element
        titleTextView = view.findViewById(R.id.infoFragmentTitleTextView);
        descriptionTextView = view.findViewById(R.id.infoFragmentDescriptionTextView);

        // Get the bundled reminder info to display
        String title = getArguments().getString("TITLE");
        String description = getArguments().getString("DESCRIPTION");

        //Set the values for the textviews from the bundled data
        titleTextView.setText(title);
        descriptionTextView.setText(description);

        return view;

1 个答案:

答案 0 :(得分:0)

我在我的活动中编写了以下代码,并在对话框片段中保存了与您相同的代码,并且其工作非常精细

     FragmentManager fm = getSupportFragmentManager();
    InfoFragment infoFragment = new InfoFragment();
    Bundle bundle = new Bundle();
    bundle.putString("TITLE", "Your title");
    bundle.putString("DESCRIPTION", "Your description");
    infoFragment.setArguments(bundle);

    // Show DialogFragment
    infoFragment.show(fm, "InfoFragment");

你能告诉我你在哪里编写代码来打开你的片段