REST and listviews updates

In my application, I have different activities with listviews. The data comes from the server using the REST method, and it only executes once when I launch the application.

The template I would like to install is to preload all the lists using JSON, which I already have on the local network, and simultaneously run a stream that receives new JSON files with my REST methods, and then update the ListViews.

So far, when I launch the application, I parse the JSON files and build all the lists of objects. I access them later in a static way, from my adapter lists.

So, I would like to know how best to run this REST stream and update the listview. Should I use AsyncTask? Service? and then when I update my local JSONs, do I need to reanalyze them, update the object lists and call my NotifyDataChanged adapters?

thank

+3
source share
2 answers

So, I found the perfect solution for my problem.
I use sendBroadcast () in onPostExecute () of my AsyncTask, with broadcastReceiver in all of my actions that needed to be notified:

public class ConvenientActivity extends Activity {
    ...
    ...
    public class RefreshReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(MyMainClass.REFRESH_INTENT)) {
                mAdapter.refreshList();
            }
        }
    }
}

It is quite simple and works great.

+1
source

So it seems that the question needs details, so I'm going to explain what the problem is.

, splashscreen, JSONs , . , , , JSON.

, , , -, JSON. , , . , .

async

class UpdateTask extends AsyncTask{
    @Override
    protected Object doInBackground(Object... params) {
        // Gets all the JSON Files and save them in local
        mJsonClient.loadDistantJSON();

        // Updates the cache maps and lists
        fillsMaps();
        fillsLists(); 
        return null;
    }

    protected void onPostExecute(Object downloadReturn) {
        // Notify the listviews
            ;
    }
}

, , :

    myAdapter = new MyBaseAdapter(this,
        SplashScreen.list,listview);

, .

0
source

Source: https://habr.com/ru/post/1756009/


All Articles