0

I'm using Azure Mobile Services to support my Android app and in this scenario, I'm trying to retrieve some database data using this method:

new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    final MobileServiceList<ActivityTable> result = activitiesTable.execute().get();
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (!(result.isEmpty())) {
                                for (ActivityTable item : result) {

                                    final ActivityTable activity = new ActivityTable();
                                    activity.setID(item.getId());
                                    activity.setContent(item.getContent());
                                    activity.setUserID(item.getUserID());
                                    activity.setMedia(item.getMedia());
                                    activity.setType((item.getType()));

                                    UserTable user = GetUser(item.getUserID());
                                    activity.setUser(user);

                                    activities.add(activity);

                                }
                                //Populate ListView
                                PopulateNewsreel();
                            } else {
                                Log.e("test", "Error in Activity Loading 1");
                            }
                        }
                    });
                } catch (Exception exception) {
                    Log.e("test","Error in Activity Loading 2");
                    Log.e("test",exception.toString());
                }
                return null;
            }
        }.execute();

And then I populate my listview using the retrieved data:

ListView newsreel = (ListView) getActivity().findViewById(R.id.newsreel_list);
NewsfeedAdapter adapter = new NewsfeedAdapter(getActivity(),R.layout.activity_newsfeed,activities);
newsreel.setAdapter(adapter);

However, the listview population is delayed and sometimes no data is shown in the listview but it takes time. After a delay of about 3 seconds, the data is populated, until then the listview populates the listview items with their placeholders. You get the idea.

How can I wait till the retrieval is completed before showing the view so that I can populate the listview?

Jay
  • 4,873
  • 7
  • 72
  • 137
  • Why Dont you use onPostExecute Method of AsyncTask to update the UI. – Dhaval Patel May 21 '15 at 09:51
  • @DhavalPatel I'm not familiar with it. Could you post an answer please sir? – Jay May 21 '15 at 09:52
  • activitiesTable.execute().get() is very bad way to get result from asynctask. As it will Waits if necessary for the computation to complete, and then retrieves its result. So during that time your UI call will be blocked. – Dhaval Patel May 21 '15 at 09:55
  • @DhavalPatel I see. What's the fix? – Jay May 21 '15 at 09:56

0 Answers0