android – Picasso和适配器与viewHolder

我和毕加索一起工作有问题.我无法从Target类访问viewHolder.保存下载的图片时出现问题.有人可以帮帮我????请.

这是适配器的代码:

   public AsanasAdapter(List<Asana> asanaList, Context mContext){

        this.mContext = mContext;
        this.asanaList = asanaList;
        this.layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    @Override
    public int getCount() {
        return asanaList.size();
    }

    @Override
    public Asana getItem(int position) {
        return asanaList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

         ViewHolder viewHolder =null;


        if(convertView==null){

            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.asanas_item, null);
            viewHolder.asanaName = (TextView) convertView.findViewById(R.id.asana_name);
            viewHolder.asanaDesc = (TextView) convertView.findViewById(R.id.asana_desc);
            viewHolder.asanaImg = (ImageView) convertView.findViewById(R.id.asana_img);
            convertView.setTag(viewHolder);

        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String name = getItem(position).getName();
        String desc = getItem(position).getDescShort();

        viewHolder.asanaDesc.setText(desc);
        viewHolder.asanaName.setText(name);

        final File imgFile = new File(Environment.getDataDirectory()+"/"+getItem(position).getId()+".png");

        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            viewHolder.asanaImg.setImageBitmap(myBitmap);

        }
        else if (isNetworkConnected()) {

            Log.d("MYLOGGGGGGGG", MYurl.BASE_URL + "/" + getItem(position).getImgThumb() + "");
            final ViewHolder finalViewHolder = viewHolder;
            Picasso.with(mContext)
                    .load(MYurl.BASE_URL + "/" + getItem(position).getImgThumb())
                    .into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            viewHolder
                            File fPath = Environment.getDataDirectory();
                            File file = new File(fPath, getItem(position)+".png");
                            if (!file.exists()) {
                                try {
                                    file.mkdirs();
                                    file.createNewFile();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                            FileOutputStream out = null;
                            try{
                                out = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
                            }
                            catch (Exception e){
                                e.printStackTrace();
                            }
                            finally {
                                try{
                                    if (out != null){
                                        out.close();
                                    }
                                }
                                catch (IOException e){
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {

                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {

                        }
                    });

        }
        else{
         //   viewHolder.asanaImg.setImageBitmap();


        }


        return convertView;
    }



    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            return false;
        } else
            return true;
    }

    static class ViewHolder {
        ImageView asanaImg;
        TextView asanaName;
        TextView asanaDesc;


    }



}

解决方法:

这里创建一个实现Target的新类,如:

public class MyTarget implements Target {
ViewHolder holder;
public MyTarget(ViewHolder holder){
    this.holder = holder;
}

@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}

}

并在您的代码中使用它,如:

 MyTarget target = new MyTarget(viewHolder) {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            //Use ViewHolder here
            File fPath = Environment.getDataDirectory();
            File file = new File(fPath, getItem(position)+".png");
            if (!file.exists()) {
                try {
                    file.mkdirs();
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            FileOutputStream out = null;
            try{
                out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
            }
            catch (Exception e){
                e.printStackTrace();
            }
            finally {
                try{
                    if (out != null){
                        out.close();
                    }
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    };
    Picasso.with(mContext)
            .load(MYurl.BASE_URL + "/" + getItem(position).getImgThumb())
            .into(target);
上一篇:图解设计模式(5) 适配器模式


下一篇:Java – 在自定义适配器中创建对两个模型的引用