Recycler查看onBindViewHolder

时间:2016-01-30 16:25:10

标签: android

我进行API调用并获得响应并将其存储在(defn render-row [] (r/as-element [ui/view])) 中。 我有1 ArrayList和1 Textview来填充。 现在,当我收到响应时,它是根据JSON(即按顺序) 但是当我尝试在我的适配器中设置它时,它是随机的(即无订单)

这是按顺序:

ImageView

现在在此适配器中,ImagePath不符合顺序:

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);

}

1 个答案:

答案 0 :(得分:0)

解析您的回复,如下所示,

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类中提到了变量,您需要为这些变量自动生成getters-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);
}
相关问题