如何使用ViewHolder在ListView上添加个人资料图片

时间:2015-03-09 12:37:59

标签: android listview android-asynctask photo

我希望能够在listView上添加每个人姓名的个人资料图片,我正在通过AsyncTask下载照片,我不知道如何显示它并在ViewHolder上找到它,图片网址已保存在String PhotoURL上,并使用新的LoadPhoto(imageView).execute(PhotoURL)下载;但是如何将这个imageView放在ViewHolder上.... 以下是使用AsyncTask,ViewHolder以及获取网址的方法的代码......:D

@Override
    public void onResult(People.LoadPeopleResult peopleData) {


        if (peopleData.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
            mCirclesList.clear();
            PersonBuffer personBuffer = peopleData.getPersonBuffer();

            try {
                int count = personBuffer.getCount();
                for (int i = 0; i < count; i++) {
                   aa.notifyDataSetChanged();
                   String PhotoURL=personBuffer.get(i).getImage().getUrl();
                   String NameURL=personBuffer.get(i).getDisplayName();
                   mCirclesList.add(NameURL+imageView);
                   new LoadPhoto(imageView).execute(PhotoURL);

                    Log.i("Connection", "Requesting visible circles: ");
                }
            } finally {
                personBuffer.close();
            }

        } else {
            Log.i("Connection", "Error requesting visible circles: " + peopleData.getStatus());
        }
    }

   public class ViewHolder{
        public ImageView profilePhoto;
        TextView profileName;
    }

    class FancyAdapter extends ArrayAdapter<String>{

        FancyAdapter(){
            super(MainActivity.this,R.layout.custom_list_item,mCirclesList);
        }
        public View getView(int position,View convertView, ViewGroup parent){

            ViewHolder holder;
            if (convertView==null){
                LayoutInflater inflater=getLayoutInflater();
                convertView=inflater.inflate(R.layout.custom_list_item,null,false);
                holder=new ViewHolder();
                holder.profileName=(TextView) convertView.findViewById(R.id.textView);
                holder.profilePhoto=(ImageView) convertView.findViewById(R.id.imageView);
                convertView.setTag(holder);
                Log.i("ViewHolder","Using ViewHolder");
            }else{
                holder=(ViewHolder) convertView.getTag();

                Log.i("ViewHolder","Recycling Shit");
                            }
            holder.profileName.setText(mCirclesList.get(position));
            holder.profilePhoto.

            return convertView;

        }
        }

      private class LoadPhoto  extends AsyncTask<String, Void, Bitmap> {
        public ImageView bmImage;

       public LoadPhoto(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);

            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

2 个答案:

答案 0 :(得分:0)

您可以使用可用的着名库来显示网络图像。 UniversalImageLoader

无需下载运行时图像。然后你可以轻松地将图像设置为imageview。

ImageLoader.getInstance().displayImage(imageURL, holder.profilePhoto);

答案 1 :(得分:0)

我已经通过在我的项目中添加Picasso库来解决这个问题