简体   繁体   English

带有联系人图片的 Android 自定义 ListView

[英]Android Custom ListView with contact image

I need to display contact picture of users in my custom ListView.我需要在我的自定义 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.我可以通过 AsyncTask 获取图像,但我无法设置图像,因为在 AsyncTask 完成之前,列表视图已经加载。 I'm calling the AsycTask for each row of the listview.我正在为列表视图的每一行调用 AsycTask。 I have the Image Uri in hand after the AsyncTask has finished.在 AsyncTask 完成后,我手头有 Image Uri。 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.如果我使用 AsyncTask().get() 方法,我可以填充 ImageView,但它会阻止 UI 线程并使 AsyncTask 的目的无效。

You could load the ListView in the onPostExecute() method of your AsyncTask.您可以在 AsyncTask 的onPostExecute()方法中加载 ListView。 This way, You will guarantee that Async task will be completed before your ListView is loaded.这样,您将保证在加载 ListView 之前完成异步任务。

You could show a ProgressDialog while the AsyncTask is executing to let the user know that data fetching is taking place.您可以在 AsyncTask 执行时显示ProgressDialog以让用户知道正在获取数据。

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() .这取决于您使用的ListAdapter ,但例如,如果您使用的是ArrayAdapter ,我认为您只需调用ArrayAdapter.notifyDataSetChanged() ,它将使用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.一个更复杂的选项是保存对ImageView的引用,但这会有点棘手,因为列表只保留可见的列表行的视图。

At last i was able to find the problem.. The onPostExecute() method of my AsyncTask class is not getting executed after doInBackground().最后我找到了问题。在 doInBackground() 之后,我的 AsyncTask 类的 onPostExecute() 方法没有被执行。 The reason it is not called is because it can take only Object parameters, like onPostExecute(Object.. args).没有调用它的原因是因为它只能接受 Object 参数,例如 onPostExecute(Object.. args)。 All my Image setting code was written inside onPostExecute().我所有的图像设置代码都写在 onPostExecute() 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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