Exchange of information with Fragments

Asked

Viewed 48 times

0

I have a CRUD to add users but I’m having trouble implementing the edit. User clicking edit wanted edittext to be loaded already filled.

For that, 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 click on the icon to edit, I wanted the screen already came with the information loaded to give the update.

I got this from the Internet:

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


        }
    }

Someone can help me?

  • Maybe this link can help you. https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments

1 answer

1


Simple, see that in onClickListener you are passing a Bundle as Arguments for Ragment:

fragment.setArguments(data);

So in Fragment you have to read from the Arguments. Do in the onViewCreated method of your reading using getArguments();

getArguments().getString("nome", "");
getArguments().getString("email", "");
getArguments().getString("np", "");
getArguments().getString("tipoFunc", "");
getArguments().getInt("id", 0);
  • But how do I take this Bundle and check if it is filled? with the Intent I would do so: Intent intent = getActivity().getIntent();
 if(intent != null){
 Bundle bundle = intent.getExtras();
 if(bundle != null){

  • getArguments back a Bundle. Just check if it has something, if(getArguments() != null) {}, then inside the if Brace you take it the way I passed the types. Reference: https://developer.android.com/reference/android/app/Fragment.html#getArguments()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.