Passing the Parameters of a Fragment that uses json for another Fragment

Asked

Viewed 37 times

0

I have a screen that searches by name through an Edittext. And when searching by name it brings me a Recycleview with its results. And when I touch an element of the result, it goes to a Fragment (another screen). Only that I wanted to pass the data of the element that was played when opening the Fragment (the Fragment already opens, just does not receive the data yet). i wanted to pass the "teacher" field to another Fragment. Can someone give a force ? Below an excerpt of Json with the Fragment call:

 public static consultarListaNome newInstance(String param1, String param2) {
    consultarListaNome fragment = new consultarListaNome();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}


public void onResponse(JSONObject response) {

    progresso.hide();

    Curso curso = null;
    JSONArray json = response.optJSONArray("curso"); // nome da tabela curso


    try {
        for(int i=0; i<json.length();i++){
            curso = new Curso();

            JSONObject jsonObject = null;
            jsonObject = json.getJSONObject(i);

            curso.setNome(jsonObject.optString("nome"));
            curso.setProfessor(jsonObject.optString("professor"));
            curso.setCategoria(jsonObject.optString("categoria"));
            curso.setDado(jsonObject.optString("imagem"));
            listaCursos.add(curso);
        }

        progresso.hide();
        CursosAdapterImg adapter = new CursosAdapterImg(listaCursos,getContext(),this);
        recyclerCursos.setAdapter(adapter);

    }catch (JSONException e){
        e.printStackTrace();
        progresso.hide();
        Toast.makeText(getContext(), "Não foi possível listar os cursos " +response , Toast.LENGTH_SHORT).show();

    }


}

This section below calls the other Fragment:

@Override
public void onNoteClick(int position) {

    listaCursos.get(position);

    //Falta passar os parâmetros

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_main,new consultarCursoUrl()).commit();


}

1 answer

0


Luciana, the correct communication between fragments would be done by the parent Activity so that one fragment does not depend on the other, and so you can reuse it without problems.
But if in your case you have no problems for this fragment (which will display the teacher field) to be dependent on another, then you can do as follows.

@Override
public void onNoteClick(int position) {

    listaCursos.get(position);

    consultarCursoUrl fragmentConsulta = new consultarCursoUrl();
    Bundle arguments = new Bundle();
    arguments.putString( "nomeProfessor" , "João da Silva");
    fragmentConsulta.setArguments(arguments);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_main,fragmentConsulta).commit();

}

And in its fragment consultarCursoUrl you can take this data like this:

Bundle arguments = getArguments();
String nomeProfessor = arguments.getString("nomeProfessor");

If you want to know about communication between Factments using the parent Activity, take a look in this matter

  • It wasn’t quite what I wanted, but at least I’ve learned to pass parameters through Ragments. I could get close. I think I explained myself badly. When I do the search, it returns me the items in the recycleview. The recycleview consists of textview (there is the teacher field) and an image. Dae interests me to take an item from this textview.

Browser other questions tagged

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