Picasso和适配器与viewHolder

时间:2015-09-28 13:00:03

标签: android adapter picasso

我与毕加索合作有问题。我无法从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;


    }



}

2 个答案:

答案 0 :(得分:4)

这里创建一个实现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);

答案 1 :(得分:1)

你不能在Target类中使用ViewHolder,因为它是一个内部类。为了访问ViewHolder,U可能必须使ViewHolder变量成为最终变量。希望它可以帮助你。