Picasso库中的错误

时间:2016-09-09 12:20:12

标签: android json picasso

我试图在Picasso中使用RecyclerView显示一个ArrayList,但我对Picasso有疑问。
如何将ArrayList中的数据加载到Picasso?

这是适配器

public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewAdapter.ViewHolder> {
List<ImagesModule> imagesModules;
Context context;

public ImageViewAdapter(List<ImagesModule> imagesModules, Context context){
    super();
    this.imagesModules = imagesModules;
    this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imageitem, parent,false );
    ViewHolder viewHolder = new ViewHolder( v );

    return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ImagesModule imagesModule = imagesModules.get( position );
    Picasso.with(context).load(String.valueOf(imagesModule.getAllimage()).into(holder.appImage));
}
@Override
public int getItemCount() {
    return imagesModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView appImage;
    public ViewHolder(View itemView) {
        super( itemView );
        appImage = (ImageView) itemView.findViewById( R.id.appimages);
     }}

这是模块

public class ImagesModule

 {
public List<String> getAllimage() {
    return Allimage;}
public void setAllimage(List<String> allimage) {
    Allimage = allimage;}
List<String> Allimage = new ArrayList<String>();
  }

在这里我获得了JSON并将其添加到ArrayList

JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONArray imageArray = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image");
    List<String> allimage = new ArrayList<String>();


    for (int j=0; i<imageArray.length(); j++) {
        JSONObject actor = imageArray.getJSONObject(i);
        String name = actor.getString("label");
        allimage.add(name);
        imagesModule.setAllimage(allimage);
    }

2 个答案:

答案 0 :(得分:0)

您需要将一个图片网址加载到Picasso中,您无法加载List。

此外,这个类基本上是一个List,所以只需将其视为一个。

public class ImagesModule extends ArrayList<String> {

}

解析 - 两个选项,一个模块用于所有图像,或以某种方式处理每个条目数据点一个模块

// TODO: Determine if you want one 'master' module for all the images
ImagesModule module = new ImagesModule(); 

JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
for (int i = 0; i < jsonArray.length(); i++) {
    // TODO: Determine if you want one module 'per entry'
    /*** ImagesModule module = new ImagesModule(); ***/
    JSONArray imageArray = jsonArray.getJSONObject(i).getJSONArray("im:image");

    for (int j=0; i<imageArray.length(); j++) {
        JSONObject actor = imageArray.getJSONObject(i);
        String name = actor.getString("label");
        module.add(name); // TODO: Actually get an image, not an actor probably...
    }

现在,您可以将该字符串加载到Picasso

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ImagesModule imagesModule = imagesModules.get( position );

    final String img = imagesModule.get(position);
    Picasso.with(context).load(img).into(holder.appImage));
}

答案 1 :(得分:0)

使用onBindViewHolder()

中的以下代码
Picasso.with(context).load(imagesModule.getAllimage().get(position).into(holder.appImage);