android ListView中通过号码查询图片,名字分条显示
由于每个item查询Contacts数据库如果单独开Thread非常耗资源,还容易卡顿,而且还容易产生显示错乱,所以要采用线程池,现在采用AsyncTask,它是与线程池 + Handler的结合 体
核心 代码如下:
通过号码查询联系人实时显示图片
public class PhotoLoaderTask extends AsyncTask<String, Void, ContactsItem> {
private String mNumber = null;;
private final WeakReference<QuickContactBadge> badgeReference;
private final WeakReference<TextView> textViewReference;
public PhotoLoaderTask(QuickContactBadge quickContactBadge, String number, TextView textview) {
badgeReference = new WeakReference<QuickContactBadge>(quickContactBadge);
textViewReference = new WeakReference<TextView>(textview);
mNumber = number;
}
@Override
// Actual download method, run in the task thread
protected ContactsItem doInBackground(String... params) {
return getPhotoAndName(mNumber);
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(ContactsItem contactsItem) {
if (isCancelled()) {
contactsItem = null;
}
if (badgeReference != null) {
QuickContactBadge quickContactBadge = badgeReference.get();
// Change bitmap only if this process is still associated with
if (contactsItem != null && quickContactBadge != null && contactsItem.uri != null) {
mContactPhotoManager.loadPhoto(quickContactBadge, contactsItem.photoId
, false, false);
quickContactBadge.assignContactUri(contactsItem.uri);
}
TextView textView = textViewReference.get();
if (contactsItem != null && textView != null) {
textView.setText(contactsItem.name);
textView.setVisibility(View.VISIBLE);
}
}
}
}
public void loadPhoto(QuickContactBadge quickContactBadge, String number, TextView textview) {
if (cancelPotentialDownload(number, quickContactBadge)) {
PhotoLoaderTask task = new PhotoLoaderTask(quickContactBadge, number, textview);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
quickContactBadge.setImageDrawable(downloadedDrawable);
textview.setText("");
textview.setVisibility(View.GONE);
task.execute();
}
}
private static boolean cancelPotentialDownload(String url, QuickContactBadge quickContactBadge) {
PhotoLoaderTask bitmapDownloaderTask = getBitmapDownloaderTask(quickContactBadge);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.mNumber;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// the uri is alerady in the task
return false;
}
}
return true;
}
private static PhotoLoaderTask getBitmapDownloaderTask(QuickContactBadge quickContactBadge) {
if (quickContactBadge != null) {
Drawable drawable = quickContactBadge.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
class DownloadedDrawable extends BitmapDrawable {
private final WeakReference<PhotoLoaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(PhotoLoaderTask bitmapDownloaderTask) {
super(bitmap);
bitmapDownloaderTaskReference =
new WeakReference<PhotoLoaderTask>(bitmapDownloaderTask);
}
public PhotoLoaderTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}