I have issue to show data in the recycle view in fragment

Viewed 53

I tested creating the user in local and it works properly, but when I try to obtain JSON, it shows the layout but doesn't show the info in the layout. So I think the problem is in the JSON parse function. I tried to use other JSON to do some tests, but I can't get the JSON info.

Fragment

public class ListaTrabajadores extends Fragment {
    ArrayList<Usuario> listaFragment;
    RecyclerView recycle;
    private   adaptador myadapter;
    private RequestQueue queu;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View vista = inflater.inflate(R.layout.fragment_lista_trabajadores, container, false);
        recycle = vista.findViewById(R.id.recyclerview);
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        listaFragment = new ArrayList<>();
         queu = Volley.newRequestQueue(getContext());
        llenarlista();

        return vista;
    }
    private void llenarlista() {
        String url = "https://pixabay.com/api/?key=myApiKey&q=kitten&image_type=photo&pretty=true";
        JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, url,null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray= response.getJSONArray("hits");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject object = jsonArray.getJSONObject(i);
                          String imagen=object.getString("pageURL");
                          String  nombre= object.getString("user");
                          String  puesto =object.getString("tags");
                          String apellido = object.getString("user");
                            listaFragment.add(new Usuario(nombre,apellido,puesto,imagen));
                        }
                        myadapter = new adaptador(getContext(), listaFragment);
                        recycle.setAdapter(myadapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }



                }


        }, new Response.ErrorListener() {
                public void onErrorResponse (VolleyError error) {
                    Log.d("tag", "onErrorResponse" + error.getMessage());
                }

        });
        queu.add(jsonArrayRequest);

    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    ArrayList<Usuario> listaActividad;
    RecyclerView recycle;
    ListaTrabajadores fragmentolista = new ListaTrabajadores();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ponerFragmento(fragmentolista);
    }

    private  void ponerFragmento(Fragment fragmento){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.contenedor, fragmento);
        fragmentTransaction.commit();
    }
}
1 Answers

I think that you are doing a network operation on the background thread and even if you are adding a new object to the listaFragment, ui does not know about it. Therefore, you should change the ui after changing the listaFragmenta. You can do it by using a ViewModel class and put the listaFragmenta there(as a MutableLiveData), then in the onCreateView function, create an observer that will be notified when this listaFragment changes. I prefer this way but you can also use an interface to communicate with ui.

Related