简体   繁体   中英

Android Custom ListView with contact image

I need to display contact picture of users in my custom ListView. I'm able to fetch images via AsyncTask, but i'm not able to set the Image because the listview will already be loaded, before AsyncTask finishes. I'm calling the AsycTask for each row of the listview. I have the Image Uri in hand after the AsyncTask has finished. Please give some ideas on how to do it.

I am using a custom base adapter to do this. I am able to populate the ImageView if I use AsyncTask().get() method but it blocks the UI thread and voids the purpose of AsyncTask.

You could load the ListView in the onPostExecute() method of your AsyncTask. This way, You will guarantee that Async task will be completed before your ListView is loaded.

You could show a ProgressDialog while the AsyncTask is executing to let the user know that data fetching is taking place.

It depends on the ListAdapter you are using, but for example if you are using an ArrayAdapter , I think you can just call ArrayAdapter.notifyDataSetChanged() and it will reload all you (visible) views using ArrayAdapter.getView() .

A more complication option would be to save references to the ImageView s but it would be a bit tricky because lists only keep views around for the list rows that are visible.

At last i was able to find the problem.. The onPostExecute() method of my AsyncTask class is not getting executed after doInBackground(). The reason it is not called is because it can take only Object parameters, like onPostExecute(Object.. args). All my Image setting code was written inside onPostExecute().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM