iBobb February 2016

JSON Download @ onCreateView leaves recyclerView empty

    if (isConnected()) {

        Event  eInstance = new Event();
        theEvents = eInstance.downloadEvents(eventsNightlife, getActivity());
        rAdapter = new RecyclerAdapter(theEvents);
        recyclerView.setAdapter(rAdapter);
        progrsBar.setVisibility(View.GONE);
        ....

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

    rAdapter.notifyDataSetChanged();

    swipeRefreshLayout.setRefreshing(false);
}

but it doesn't work. I also tried to

rAdapter = new RecyclerAdapter(theEvents);
    rAdapter.notifyDataSetChanged();
    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>() {

                       

Answers


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

             ...
             @Override
             public void onResponse(JSONArray response) {
                 super.onResponse(response);
                 if(respone!=null){
                     // Do what i want with my received data
                     dlDataEvent.setData(response);
                 }
                 // Post my event by EventBus
                 EventBus.getDefault().post(dlDataEvent);
              ...

         }
    }
}

This is my event:

public class DownloadDataEvent{
    private JSONArray mData;

    public void setData(JSONArray data){
         mData=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 {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         ...

         // I have to register this class for EventBus subscriber:
         if(!EventBus.getDefault().isRegister(this)){
              EventBus.getDefault().registerSticky(this);
         }

         // Call my downloadData method
         if(isConnected()){
              MyDownloader.downloadData();
  

Post Status

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

Search




Leave an answer