Home Ask Login Register

Developers Planet

Your answer is one click away!

iBobb February 2016

JSON Download @ onCreateView leaves recyclerView empty

    if (isConnected()) {

        Event  eInstance = new Event();
        theEvents = eInstance.downloadEvents(eventsNightlife, getActivity());
        rAdapter = new RecyclerAdapter(theEvents);

This is part of the code that runs at "onCreateView". The method downloadEvents uses Volley to download JSON data, extract it and return a list of items (theEvents). Now when my app starts, the recycler view is empty. If I go to my home screen out of the app and then run my app again, this time the data sometimes gets downloaded.

I debugged step by step, and at first launch (i mean when the app is not just resuming), theEvents is empty, so the download didn't return or manage to return anything...

Suggestions on how to execute things before the UI has been shown to the user or what actually needs to be done to approach this task better?

Also, I use a swipeRefreshLayout and at its onRefresh method I do:

public void onRefresh() {
    Event  eInstance = new Event();
    theEvents = eInstance.downloadEvents(eventsNightlife, getActivity());



but it doesn't work. I also tried to

rAdapter = new RecyclerAdapter(theEvents);
    recyclerView.swapAdapter(rAdapter, false); 

still not working.

EDIT: My downloadEvents method implementing Volley:

public List<Event> downloadEvents(String urlService, Context context) {
    eventsList = new ArrayList<>();
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
            (Request.Method.GET, urlService, null, new Response.Listener<JSONArray>() {



Seyyed February 2016

You can use EventBus for your purpose that is a simple and truth way.
Here, i write an example for how to use EventBus with volley.
Consider that i want to download some data.

This is the class that my download methods is inside it (you can add more methods to it in the future):
Im used volley to download my data:

// Download methods is inside volley
public class MyDownloader{

    public static void downloadData(){
         DownloadDataEvent dlDataEvent=new DownloadDataEvent();
         List<String> myResult=new ArrayList<>();

             public void onResponse(JSONArray response) {
                     // Do what i want with my received data
                 // Post my event by EventBus


This is my event:

public class DownloadDataEvent{
    private JSONArray mData;

    public void setData(JSONArray data){

    public JSONArray setData(){
        return mData;


Now i want to use my downloadData() method inside my MainActivity:
(I called my downloadData method inside onCreate.)

public class MainActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState) {

         // I have to register this class for EventBus subscriber:

         // Call my downloadData method

Post Status

Asked in February 2016
Viewed 2,378 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved