适配器数据交替更改

时间:2016-02-01 08:19:36

标签: android

情况是适配器结果交替变化 即

首先是: [http://cache3.asset-cache.net/xt/540053243.jpg?v=1&g=fs1|0|FPG|53|243&s=1&b=RjI4, 然后: [http://cache1.asset-cache.net/xt/471950448.jpg?v=1&g=fs1|0|EPL|50|448&s=1&b=RjI4

进行相同的网络通话。

这是我的Adapter类:

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

有趣的是Title字段是相同的。

响应类:

 private ModelGettyImages getImageDetails(String jsonData) throws JSONException {

    boolean ascending = sharedPreferences.getBoolean("Ascending",false);
    Log.d("Ascending",Boolean.toString(ascending));
    boolean descending = sharedPreferences.getBoolean("Descending",false);
    Log.d("descending",Boolean.toString(descending));

    String pathURI = "";
    JSONObject imageData = new JSONObject(jsonData);
    int resultCount = imageData.optInt("result_count");
    Log.d("Result Count: ", Integer.toString(resultCount));

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

        }

        if( ascending )
        {
            Log.d("Ass","True");

            Collections.sort(allTitles, new Comparator<String>() {
                @Override
                public int compare(String s1, String s2) {
                    return s1.compareToIgnoreCase(s2);
                }
            });
        }
        else if( descending )
        {
            Log.d("Des","True");

            Collections.sort(allTitles, new Comparator<String>() {
                @Override
                public int compare(String s1, String s2) {
                    return s2.compareToIgnoreCase(s1);
                }
            });
        }


        Log.d("Image Title Sort: ", allTitles.get(i));

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




    }


    return modelGettyImages;
}

1 个答案:

答案 0 :(得分:0)

尝试在加载url之前重置imageview使用此代码

Picasso.with(this.context).cancelRequest(holder.image);

Picasso.with(context).load(image.get(position)).fit().centerCrop().into(holder.image);