JosCarrillo
JosCarrillo

Reputation: 85

RecyclerView fill it with a Volley request doesn't show results

I'm having a problem with Volley and the RecyclerView.

In the onCreate Method of my Activity, I call a Volley Request to get data from my server. I then pass it to my Adapter as a List.

The thing is that everything is happening so fast when the onCreate method has finished my Volley Request hasn't finished yet, so nothing is displayed on the screen, how can I avoid this?

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__services__worker);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Servicios");
        final List services = new ArrayList();

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Method.GET, AppConfig.URL_GET_SERVICES_REQUIRED, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            try{
                JSONArray jsonArray = new JSONArray(response);
                for(int i=0; i<jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String tag_servicio = jsonObject.getString("contratista");
                    String servicio = jsonObject.getString("descipcion_del_problema");
                    String zona = jsonObject.getString("zona");
                    String money = jsonObject.getString("precio_min");
                    //Toast.makeText(Activity_Services_Worker.this, tag_servicio+servicio+zona+money, Toast.LENGTH_SHORT).show();
                    services.add(new Servicios_worker(tag_servicio, servicio, zona, money));
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse (VolleyError error){
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(Activity_Services_Worker.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }); 
    requestQueue.add(stringRequest);



    recycler = (RecyclerView) findViewById(R.id.servicesAsked_RecyclerView);
    recycler.setHasFixedSize(true);

    lManager = new LinearLayoutManager(this);
    recycler.setLayoutManager(lManager);

    adapter = new Adapter_services_required(services);
    recycler.setAdapter(adapter);


    }

Upvotes: 1

Views: 568

Answers (1)

joao86
joao86

Reputation: 2076

In the add method, which I think is adding an element to the array of elements of the adapter, you should do notifyDatasetChanged() or you can do this:

...
final List services = new ArrayList();

;; CHANGED
recycler = (RecyclerView) findViewById(R.id.servicesAsked_RecyclerView);
recycler.setHasFixedSize(true);

lManager = new LinearLayoutManager(this);
recycler.setLayoutManager(lManager);

adapter = new Adapter_services_required(services);
recycler.setAdapter(adapter);

RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Method.GET, AppConfig.URL_GET_SERVICES_REQUIRED, new Response.Listener<String>(){
    @Override
    public void onResponse(String response){
        try{
            JSONArray jsonArray = new JSONArray(response);
            for(int i=0; i<jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String tag_servicio = jsonObject.getString("contratista");
                String servicio = jsonObject.getString("descipcion_del_problema");
                String zona = jsonObject.getString("zona");
                String money = jsonObject.getString("precio_min");
                //Toast.makeText(Activity_Services_Worker.this, tag_servicio+servicio+zona+money, Toast.LENGTH_SHORT).show();
                services.add(new Servicios_worker(tag_servicio, servicio, zona, money));
                ;; CHANGED
                adapter.notifyDatasetChanged(); 
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener(){
    @Override
    public void onErrorResponse (VolleyError error){
        Log.e(TAG, "Registration Error: " + error.getMessage());
        Toast.makeText(Activity_Services_Worker.this, error.getMessage(), Toast.LENGTH_LONG).show();
    }
}); 
requestQueue.add(stringRequest);

Upvotes: 4

Related Questions