使用捆绑包将textview从一个片段更新为另一片段不起作用

时间:2019-03-27 11:32:20

标签: android android-fragments android-fragmentactivity

我遵循了使用捆绑软件将数据从一个片段发送到另一个片段的代码示例,但是我想在其中显示包含字符串的捆绑软件值的textview中未显示存储的值。

该字符串值正在获取用户从列表视图中单击的电话名称联系人,下面的祝酒词显示该字符串已成功选择了该名称。

在示例之后,我已经重写了多次将字符串值发送到片段的代码,但是我没有运气。

SelectModemFragment

 // Set the ListViews OnItemClick Listener
            display_contacts1.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    String namedisplay = arrayAdapter.getItem(position); //<<<<<<<<<< this gets the phone name

                    Toast.makeText(view.getContext(), namedisplay + " Selected for Communication", Toast.LENGTH_SHORT).show();
                    Toast.makeText(view.getContext(), phoneNo, Toast.LENGTH_SHORT).show();

                    // Using bundle to send data
                    Bundle bundle = new Bundle();
                    bundle.putString(namedisplay, "namevalue");

                    // Begin transaction to command window
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    CommandsFragment modemFragment = new CommandsFragment();
                    modemFragment.setArguments(bundle); // Data to be send to commands fragment
                    transaction.replace(R.id.frame_layout, modemFragment);
                    transaction.commit();

                    //modemView.setText(namedisplay);
                }
            });

CommandsFragment

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

        modemView = (TextView) view.findViewById(R.id.modem_view);

        if (display_contacts.isEmpty())
        {
            number = null;

            // No contacts selected from the Add Modem listview
            modemView.setText("No contact selected");
        }
        else
        {
            Bundle bundle = getArguments();
            modemView.setText(String.valueOf(bundle.getString("namevalue")));
        }

        return view;
}

1 个答案:

答案 0 :(得分:1)

这是您的问题:

bundle.putString(namedisplay, "namevalue");

应该是

bundle.putString("namevalue", namedisplay);

先输入键,然后输入值。