简体   繁体   English

如何将捆绑数据从一个片段获取到另一个片段?

[英]How to get bundle data from one fragment to another fragment?

I have two fragments.addfragment and control fragment.我有两个 fragment.addfragment 和控制片段。 From addfragment I send the data using bundle with fragment transaction and then in control fragment,I want to get that data using bundle but I can't get data.从 addfragment 我使用带有片段事务的包发送数据,然后在控制片段中,我想使用包获取该数据,但我无法获取数据。 so plz help me所以请帮助我

Here is my addfragment with fragment transaction-这是我的带有片段事务的 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();
    }
});

and In control fragment I get data this way...but I can't get data在控制片段中,我以这种方式获取数据......但我无法获取数据

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

You are setting constructor instead of object.您正在设置构造函数而不是对象。 Use below code使用下面的代码

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 ); transaction.replace(R.id.mainFrame, fragment );

This is change.这是变化。

Do the fetching data before oncreate view in oncreate.Always do a null check error before setting data.Hope this helps you在oncreate中的oncreate视图之前做取数据。在设置数据之前总是做空检查错误。希望这对你有帮助

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
        String ip = bundle.getString("ip");
        String port = bundle.getString("port");
        String uname = bundle.getString("uname");
        String password = bundle.getString("password");
    }
}

EDIT :编辑

As Amrish Kakadiya pointed out正如阿姆里什·卡卡迪亚指出的那样

transaction.replace(R.id.mainFrame, new ControlFragment()); transaction.replace(R.id.mainFrame, new ControlFragment());

This line of code is creating a problem, new ControlFragment() creates an entirely new instance of the fragment and hence you are getting the getArguments() as null while retrieving.这行代码产生了一个问题, new ControlFragment() 创建了一个全新的片段实例,因此您在检索时将 getArguments() 设为 null。

use this line使用这条线

Bundle bundle = getArguments();捆绑包 = getArguments();

before you inflater your rootview在你膨胀你的 rootview 之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM