android ListView中通过号码查询图片,名字分条显示

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();

        }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值