【问题标题】:Recycler View onBindViewHolder回收站视图 onBindViewHolder
【发布时间】:2019-09-10 13:46:55
【问题描述】:

我进行 API 调用并获得响应并将其存储在 ArrayList 中。 我有 1 个Textview 和 1 个ImageView 要填充。 现在,当我得到响应时,它是根据 JSON(即按顺序) 但是当我尝试在我的适配器中设置它时,它是随机的(即无顺序)

这里是顺序:

JSONArray imageArray = imageData.optJSONArray("images");
for (int i = 0; i < imageArray.length(); i++)
{
    JSONObject img = imageArray.optJSONObject(i);
    allID.add(i,img.optString("id"));
    allTitles.add(i,img.optString("title"));
    Log.d("Image ID: ", allID.get(i));
    Log.d("Image Title: ", allTitles.get(i));
    JSONArray imagePath = img.optJSONArray("display_sizes");
    for (int j = 0; j < imagePath.length(); j++)
    {
        JSONObject jb = imagePath.optJSONObject(j);
        allImagePath.add(j, jb.getString("uri"));
        Log.d("Image Path: ", allImagePath.get(j));

    }

    modelGettyImages.setID(allID);
    modelGettyImages.setTitle(allTitles);
    modelGettyImages.setImagePath(allImagePath);
    ImagesList.add(modelGettyImages);

}

现在这个 Adapter 中的 ImagePath 不是按顺序排列的:

public class ImageCustomAdapter extends RecyclerView.Adapter<ImageCustomAdapter.ImagesViewHolder> {

    Context context;
    private ArrayList<ModelGettyImages> images;
    private ArrayList<ModelGettyImages> allImages = new ArrayList<>();

    public ImageCustomAdapter(ArrayList<ModelGettyImages> images,Context context) {
        this.images = images;
        this.context = context;
    }

    @Override
    public ImagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row, parent, false);
        ImagesViewHolder imagesViewHolder = new ImagesViewHolder(view);


        return imagesViewHolder;
    }

    @Override
    public void onBindViewHolder(ImagesViewHolder holder, int position) {


        List<String> text = images.get(position).getTitle();
        Log.d("TitleAdapter : ",images.get(position).getTitle().toString());
        List<String> image = images.get(position).getImagePath();
        Log.d("ImageAdapter : ", images.get(position).getImagePath().toString());
        holder.title.setText(text.get(position));
        Picasso.with(context).load(image.get(position)).fit().centerCrop().into(holder.image);




    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    public class ImagesViewHolder extends RecyclerView.ViewHolder{
        ImageView image;
        TextView title;
        public ImagesViewHolder(View itemView) {
            super(itemView);

             image = (ImageView)itemView.findViewById(R.id.imageView);
             title = (TextView)itemView.findViewById(R.id.textView);
        }
    }
}

【问题讨论】:

  • 您能分享一下您的 JSON 响应吗?

标签: android


【解决方案1】:

如下所示解析您的回复,

JSONArray imageArray = imageData.optJSONArray("images");
for (int i = 0; i < imageArray.length(); i++)
{
    JSONObject img = imageArray.optJSONObject(i);

    Log.d("Image ID: ", img.optString("id"));
    Log.d("Image Title: ", img.optString("title"));

    JSONArray imagePath = img.optJSONArray("display_sizes");

    for (int j = 0; j < imagePath.length(); j++)
    {
        JSONObject jb = imagePath.optJSONObject(j);
        allImagePath.add(j, jb.getString("uri"));
        Log.d("Image Path: ", allImagePath.get(j));

    }

    modelGettyImages.setId(img.optString("id"));
    modelGettyImages.setTitle(img.optString("title"));
    modelGettyImages.setImagePaths(allImagePath);
    ImagesList.add(modelGettyImages);
}

使用以下变量和 getters-setter 为您的模型类 ModelGettyImages 做好准备。 (我刚刚提到了 Model 类中的变量,您需要为这些变量自动生成 getter-setter)

private String id, title;
private List<String> imagePaths;

更新onBindViewHolder方法如下,

@Override
public void onBindViewHolder(ImagesViewHolder holder, int position) {

    ModelGettyImages currentImage = images.get(position);

    Log.d("TitleAdapter : ",currentImage.getTitle();
    List<String> imagePaths = currentImage.getImagePaths();

    // Below is the list of images with multiple dimension. 
    // And we are using very first dimension image from that.
    Log.d("ImageAdapter : ", imagePaths.get(0).toString());

    holder.title.setText(currentImage.getTitle());
    Picasso.with(context).load(imagePaths.get(0)).fit().centerCrop().into(holder.image);
}

【讨论】: