【问题标题】:Dialog fragment bundle not passing data对话框片段包不传递数据
【发布时间】:2017-12-13 13:46:21
【问题描述】:

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

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

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;

【问题讨论】:

    标签: android android-fragments android-bundle


    【解决方案1】:

    我在我的活动中编写了以下代码,并在对话框片段中保留了与您的代码相同的代码,并且它工作得很好

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

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

    【讨论】:

    • 在数组适配器中
    • 因此您需要在适配器构造函数中传递活动实例,例如 public MyArrayAdapter(Activity mActivity) 并使用 mActivity 对象来获取片段管理器。试试这个,如果它不起作用,请告诉我
    猜你喜欢
    • 2017-07-21
    • 2013-07-11
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多