gpt4 book ai didi

java - 如何将 bundle 数据从一个 fragment 获取到另一个 fragment ?

转载 作者:行者123 更新时间:2023-11-29 04:26:19 24 4
gpt4 key购买 nike

我有两个 fragments.addfragment 和控制 fragment 。从 addfragment 我使用带有 fragment 事务的 bundle 发送数据,然后在控制 fragment 中,我想使用 bundle 获取该数据但我无法获取数据。所以请帮助我

这是我的带有 fragment 事务的 addfragment-

     relaylist.setAdapter(adapter);
relaylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


HashMap<String, String> o = (HashMap<String, String>) relaylist.getItemAtPosition(position);



Log.e("ip: ", "> " + o.get("ip"));
Log.e("port: ", "> " + o.get("port"));
Log.e("uname: ", "> " + o.get("uname"));
Log.e("password: ", "> " + o.get("password"));


ControlFragment fragment = new ControlFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("ip", o.get("ip"));
bundle.putString("port", o.get("port"));
bundle.putString("uname", o.get("uname"));
bundle.putString("password", o.get("password"));
Log.e("ip: ", "> " + o.get("ip"));
Log.e("port: ", "> " + o.get("port"));
Log.e("uname: ", "> " + o.get("uname"));
Log.e("password: ", "> " + o.get("password"));
fragment.setArguments(bundle);
transaction.replace(R.id.mainFrame, new ControlFragment() );

transaction.commit();
}
});

在控制 fragment 中,我以这种方式获取数据......但我无法获取数据

public class ControlFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_control, container, false);
Bundle bundle = getArguments();

String ip=bundle.getString("ip");
Stirng port = bundle.getString("port");
Stirng uname = bundle.getString("uname");
Stirng password = bundle.getString("password");

最佳答案

您正在设置构造函数而不是对象。使用下面的代码

ControlFragment fragment = new ControlFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("ip", o.get("ip"));
bundle.putString("port", o.get("port"));
bundle.putString("uname", o.get("uname"));
bundle.putString("password", o.get("password"));
Log.e("ip: ", "> " + o.get("ip"));
Log.e("port: ", "> " + o.get("port"));
Log.e("uname: ", "> " + o.get("uname"));
Log.e("password: ", "> " + o.get("password"));
fragment.setArguments(bundle);
transaction.replace(R.id.mainFrame, fragment );

transaction.replace(R.id.mainFrame, fragment );

这就是改变。

关于java - 如何将 bundle 数据从一个 fragment 获取到另一个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46115790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com