Exchange information with fragments

0

I have a CRUD to add users but I'm having trouble implementing the edit. The user clicking edit wanted the edittext to be loaded already filled in.

For this, I tried to implement something like:

ImageView editarBt = (ImageView) view.findViewById(R.id.editar);
            editarBt.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View view) {

                    Bundle data = new Bundle();
                    data.putString("nome", listaUser.get(auxPosition).getNome());
                    data.putString("email", listaUser.get(auxPosition).getEmail());
                    data.putString("np", listaUser.get(auxPosition).getNp());
                    data.putString("tipoFunc", listaUser.get(auxPosition).getTipoFunc());
                    data.putInt("id", listaUser.get(auxPosition).getId());
                    Fragment fragment = new adicionar_usuario();
                    fragment.setArguments(data);
                    FragmentManager manager = getFragmentManager();
                    manager.beginTransaction().replace(R.id.content,fragment, fragment.getTag()).addToBackStack(null).commit();


                }
            });

That is, when the user clicks the icon to edit, I wanted the screen to already come with the information loaded to give the update.

I achieved this through the intent:

Intent intent = new Intent(getActivity(), adicionar_usuario.class);
                manager.putFragment(bundle,"nome", listaUser.get(auxPosition).getNome());
                intent.putExtra("email", listaUser.get(auxPosition).getEmail());
                intent.putExtra("np", listaUser.get(auxPosition).getNp());
                intent.putExtra("tipoFunc", listaUser.get(auxPosition).getTipoFunc());
                intent.putExtra("id", listaUser.get(auxPosition).getId());
                getContext().startActivity(intent);

But since it is a fragment, the intent does not become viable.

And in my fragment I want the information to be loaded (add_user), with the intent is:

 Intent intent = getActivity().getIntent();
    if(intent != null){
        Bundle bundle = intent.getExtras();
        if(bundle != null){

            usuario.setId(bundle.getInt("id"));
            usuario.setNome(bundle.getString("nome"));
            usuario.setEmail(bundle.getString("email"));
            usuario.setTipoFunc(bundle.getString("tipoFunc"));
            usuario.setNp(bundle.getString("np"));

            nomeEt.setText(usuario.getNome());
            emailEt.setText(usuario.getEmail());
            npEt.setText(usuario.getNp());
            tipoFuncSp.toString();

          //  senhaEt.setVisibility(View.GONE);
            salvarBt.setVisibility(View.GONE);
            editarUserBt.setVisibility(View.VISIBLE);


        }
    }

Can anyone help me?

    
asked by anonymous 23.06.2017 / 16:22

1 answer

1

Simple, see that in the onClickListener you are passing a Bundle like Arguments to fragment:

fragment.setArguments(data);

Then in the fragment you have to read from the arguments. Make in the onViewCreated method of your fragment the reading catching with getArguments ();

getArguments().getString("nome", "");
getArguments().getString("email", "");
getArguments().getString("np", "");
getArguments().getString("tipoFunc", "");
getArguments().getInt("id", 0);
    
26.06.2017 / 13:43