-1

i am using a custom listview with images in my app and loading all data from a json url.

i have created a onscrollistener()

which automatically add data below the current data when user scrolls to the bottom of the listview.

But when my data is loading whole listview freezes for 2-3 sec.

I dont know whats wrong??

here is my code for AsyncTask

private class BackgroundLoadMore extends AsyncTask<Void, Void, Void> {

          @Override
            protected void onPreExecute() {
                // Showing progress dialog before sending http request


            }

        protected Void doInBackground(Void... unused) {
            runOnUiThread(new Runnable() {
                public void run() {

                    LoadData();


                            list.setOnScrollListener(new EndlessScrollListener());
                }
            });
            return (null);
        }

        protected void onPostExecute(Void unused) {
            // On completing background task
            // closing progress dialog etc,.


        }
Badal
  • 3,738
  • 4
  • 33
  • 60
  • You have to download all the data in background thread. Check this: http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – dilix Jul 26 '12 at 11:26
  • 6
    Your `runOnUiThread` call makes no sense. It's like walking out a door and walking back in again. – Che Jami Jul 26 '12 at 11:31

4 Answers4

4

You must be fetching the json data from url in main UI thread. This blocks the UI from being updated by system, and hence the freeze. Use AsyncTask to do such network tasks in background.

LoadData() should be called in a background thread, which is asynctask's doInBackground(). Your call runOnUIThread puts it back on the UI thread, and that you dont want. remove the runOnUIThread call from asynctask.

     protected Void doInBackground(Void... unused) {
        LoadData();

        return (null);
    }

   protected void onPostExecute(Void unused) {
        // On completing background task
        // closing progress dialog etc,.

        list.setOnScrollListener(new EndlessScrollListener());

    }
Ron
  • 24,175
  • 8
  • 56
  • 97
1

Move LoadData(); out of

runOnUiThread(new Runnable() {
    public void run() {}
};
Todd Davies
  • 5,484
  • 8
  • 47
  • 71
0

You are probably loading your data inside the UI thread.

Since operations such as loading JSON data from the internet are slow (from 0.5-10 seconds is typical) then if you do that inside the main thread of your app, the user interface will stop responding for this time. You should use a Thread or AsyncTask to load the JSON data asynchronously and so keep the UI thread free to respond to user input (such as scrolling the list).

My suggestion is that you use an AsyncTask to load the data.

Here are some links:


Edit

Put list.setOnScrollListener(new EndlessScrollListener()); and list.notifyDatasetChanged(); inside onPostExcecute();

Community
  • 1
  • 1
Todd Davies
  • 5,484
  • 8
  • 47
  • 71
0

doInBackground is performing in the worker thread, but in it you use runOnUiThread wich start a UI therad operations.

You have to load all the data from net in background thread and then in postExecute update your listview

dilix
  • 3,761
  • 3
  • 31
  • 55