Search code examples
androidandroid-asynctaskuniversal-image-loader

Showing Dynamic Data(Images, Description) with List View of Universal Image Loader


What i want to achieve is a list view filled with images and some descriptive text associated with each item.The Image Url, Text is both dynamically fetched from web-(Json response). implementing Universal Image Loader is the possible solution.Here is what i am doing.

  1. Before setting adapter to listview in onCreate() method, start a new Asyn task to fetch data(image urls, text) from web and then store the result in respective arrays..imageUrls & descTexts.

  2. Immediately after the Async call , set the adapter to list view.

  3. In the getView() method of Adapter, i am using these imageUrls and descTexts arrays to populate list view.

Problem: in getView() method,before using the populated arrays i need to make sure that the async task has finished .How do i do this. I can use getStatus() method of async task class,but it blocks UI thread.

Any solution to this issue.Or Am i doing it the wrong way.


Solution

  • Check out the onPostExecute() method of the AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute%28Result%29).

    Just put the code to set the adapter inside this method which runs inmediately after the async taks finishes.

    So, as an example:

     class MyAsyncTask extends AsyncTask<String, Void, Void>{
    
            @Override
            protected void onPreExecute() {
            }
    
            @Override
            protected Void doInBackground(String... params) {
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                // create you adapter here and set it to the ListView
                Adapter myAdapter = ...
                myListView.setAdapter(myAdapter);
            }
        }