0

I'm searching some pages from facebook and then downloading their pictures. i want to view them immediately in my list and update the adapter everytime i download a new picture. i'm using the same method in one other activity and it works, but here i'm using a dialog and it freeze my UI till everything is finished, even if i'm doing it in background. why?

private synchronized void getUserLikesImages() {AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {

        @Override
        public Bitmap doInBackground(Void... params) {
        for(...){
               //download one picture...
               FacebookeventsActivity.this.runOnUiThread(new Runnable() {
               public void run() {
                        pagesILikeArrayAdapter.notifyDataSetChanged();
               }
            });

        }
      };
   task.execute();
   }

it works, but the UI is frozen till the end of the operations... i think it might be because my list is in a dialog and i'm running it on UIThread, but i don't know how to fix it.

user1345108
  • 230
  • 2
  • 15
  • I am not sure what logic you have implemented for downloading images and displaying inside the ListView, i would suggest to implement [**Lazy Loading images**](http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012) – Paresh Mayani Jun 20 '12 at 11:54
  • Just download your images in doInback..() of Asynctask one-by-one and when one download completes just publishProgress() which trigger onProgressUpdate() and notify your adapter in that method. – user370305 Jun 20 '12 at 12:07

1 Answers1

2
@Override
        public Bitmap doInBackground(Void... params) {
for(...){
//download one picture...

  }
 };


@Override
onPostExecute()
{
 pagesILikeArrayAdapter.notifyDataSetChanged();
}

Just add onPostExecute() and notify adapter from that.. Or just Add onProgreesUpdate() method and use publishProgress() from doInBackGround() notify adapter periodically from that..

user370305
  • 108,599
  • 23
  • 164
  • 151
  • +1/2 - Because it depends on the condition, when should we call notifyDataSetChanged(), because if user want to notify for single item fetched/parsed then he might has to implement inside `onProgressUpdated()`. Hope you will get my point. – Paresh Mayani Jun 20 '12 at 11:49
  • Again mistake, are you sure about this you have written in answer => You can not run runOnUiThread in doInBackGround() of AsyncTask** – Paresh Mayani Jun 20 '12 at 11:50
  • I tried to call notifyDataSetChanged() in the onProgressUpdate, but it still freeze.. – user1345108 Jun 20 '12 at 13:25
  • i solved it by downloading one single picture per time in a background method and then in the onPostExecute() method i call notifyDataSetChanged and again my imageDownload method in a recursive way. this way it doesn't freeze. – user1345108 Jun 28 '12 at 15:07