Async list view update in Android using Retrofit

I was recently wondering how to solve a problem of getting data asynchronously and making operations on them. I want to share my solution with you and get to know how do you cope with that problem.

Description of a problem.

Let start and see what is going on. This is simple application that visualize cards in your hand like in poker. What the application do is taking a deck from API and then it is choosing cards from that deck. App should also show how many cards have left in the deck.

In a snippet showed above, we can see synchronous way of getting data. This solution makes user waiting for data which leads to black screen showing up on device while the data is loaded. In the other hand if we want to get data in async way we will get null as a result because there is no enough time for gathering data in that single thread.

So my solution is …

Transfer an adapter as a parameter to async call.

As soon as async task has gathered data from server the adapter is going to update list view. RefreshData function from line 9 clear list that is located in the adapter, add another collection to that list and notify adapter that data was changed.

My idea was to transfer activity as a param to query function, thanks to that we can invoke methods to update view. That manipulation makes device screen loads faster and update with data while loading view in another thread. Now populate function looks like that:

What do You think and how do You solve that problem? Maybe there is a better way so please comment. Full code can be seen at