如何使用Picasso库设置imageview?

时间:2015-09-24 12:12:09

标签: android picasso

我有我的GridViewAdapter,我想用Picasso的isI加载设置一个imageview。图像被加载,但没有在GridView中显示,只有当我点击图像时才会全屏显示图像,就像我点击它一样。每个答案都是相关的。感谢。

private static LayoutInflater inflater = null;

public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
    activity = a;
    filepath = fpath;
    filename = fname;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return filepath.length;

}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}


public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
    // Locate the TextView in gridview_item.xml
    TextView text = (TextView) vi.findViewById(R.id.text);
    // Locate the ImageView in gridview_item.xml
     image = (ImageView) vi.findViewById(R.id.grid_image);

    // Set file name to the TextView followed by the position

    Picasso.with(parent.getContext()).load(filepath[position]).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);

    // Decode the filepath with BitmapFactory followed by the position


    // Set the decoded bitmap into ImageView
  //  image.setImageBitmap(bmp);
    return vi;
}

}

3 个答案:

答案 0 :(得分:1)

在此使用activity上下文

 Picasso.with(activity).load(filepath[position]).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);

答案 1 :(得分:1)

在你的getView()方法

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       View vi = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) row.findViewById(R.id.text);
            holder.imageView = (ImageView) row.findViewById(R.id.grid_image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        GridItem item = mGridData.get(position);
        holder.titleTextView.setText(Html.fromHtml(item.getTitle()));

        Picasso.with(context).load(filepath[position]).into(holder.imageView);
        return row;
    }

    static class ViewHolder {
        TextView titleTextView;
        ImageView imageView;
    }

注:

1.如果图像路径是本地的,请确保传递文件。 2.从您调用适配器的活动中传递上下文,如SampleActivity.this

并尝试将Activity对象更改为适配器构造函数中的Context对象

有关如何使用服务器中的图像加载网格视图的更多详细信息,请检查Grid Sample

答案 2 :(得分:1)

如果您的图片路径是本地的,那么您必须使用文件

File file = new File(filepath[position]);
 Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);