2017-08-29 52 views
0

我有一個項目列表(6),我正在使用畢加索和OkHttp庫在我的回收視圖中下載和顯示圖像。使用畢加索和回收站視圖來顯示圖像列表

我在我的回收站視圖中有6個項目,其中3個(1,2,4)在服務器上有圖像,項目3,5和6沒有圖像。但在我的回收商查看下載的項目1,2,4的圖像也會顯示項目3,5和6!我不知道問題在哪裏。 這是從該適配器下載和加載圖片與畢加索我的代碼:

public class BaseListAdapter extends RecyclerView.Adapter<BaseListAdapter.ViewHolder> { 
    private List<PList> menuItems; 
    private Context mContext; 
    private ActivitySingleGroup activitySingleGroup; 


    //Bottom Sheets Views Declaration 
    private TextView txtSelectedProduct; 
    private TextView txtPRemark; 
    private TextView txtQty; 
    private EditText edtUserQty; 
    private Button btnBuy; 
    private Button btnDiscard; 
    private ViewPager mPager; 
    private CircleIndicator indicator; 

    public BaseListAdapter(List<PList> menuItems, Context mContext) { 
    this.menuItems = menuItems; 
    this.mContext = mContext; 
    this.activitySingleGroup = (ActivitySingleGroup) mContext; 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 

    TextView txtName; 
    TextView txtPrice; 
    ImageView imgDefault; 
    LinearLayout parentLayout; 
    CoordinatorLayout coordinatorLayout; 

    public ViewHolder(View v) { 
     super(v); 

     txtName = (TextView) v.findViewById(R.id.txtName); 
     txtPrice = (TextView) v.findViewById(R.id.txtPrice); 
     imgDefault = (ImageView) v.findViewById(R.id.img_defaultImage); 
     parentLayout = (LinearLayout) v.findViewById(R.id.parentLayout); 
     coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout); 

    } 
    } 

    @Override 
    public BaseListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    // Create a new View 
    final View v = LayoutInflater.from(activitySingleGroup).inflate(R.layout.activity_normal_group_recycler, parent, false); 
    ViewHolder vh = new ViewHolder(v); 
    return vh; 
    } 

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


    //Download and Load Default Image from server into imgDefault ImageView 
    Picasso picasso; 
    OkHttpClient client = null; 
    String url = "https://bartarinapp-irdeveloper.rhcloud.com/api/images/download/"; 

    //Handel situations that default image variables will be null 
    if (menuItems.get(position).getPDefaultImage() != null && 
     menuItems.get(position).getPDefaultImage().getDefault() != null) { 

     if ((menuItems.get(position).getPDefaultImage().getDefault()) && 
     (menuItems.get(position).getPDefaultImage().getIId() != null)) { 

     url += menuItems.get(position).getPDefaultImage().getIId(); 

     client = ServerClass.downloadImage(
      menuItems.get(position).getPDefaultImage().getIId(), 
      holder.imgDefault, 
      activitySingleGroup); 

     picasso = new Picasso.Builder(mContext) 
      .downloader(new OkHttp3Downloader(client)) 
      .listener(new Picasso.Listener() { 
      @Override 
      public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 
       //Here your log 
       Log.i("I_ERROR", "Error is: " + exception.toString()); 
      } 
      }) 
      .build(); 

     } else { 
     url = null; 
     picasso = new Picasso.Builder(mContext) 
      .listener(new Picasso.Listener() { 
      @Override 
      public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 
       //Here your log 
       Log.i("I_ERROR", "Error is: " + exception.toString()); 
      } 
      }) 
      .build(); 

     } 
    } else { 
     url = null; 
     picasso = new Picasso.Builder(mContext) 
     .listener(new Picasso.Listener() { 
      @Override 
      public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 
      //Here your log 
      Log.i("I_ERROR", "Error is: " + exception.toString()); 
      } 
     }) 
     .build(); 
    } 


    picasso.cancelRequest(holder.imgDefault); 
    if (url != null && url.length() > 0) { 

     //put here picaso image load code 
     picasso.load(url) 
     .placeholder(R.drawable.loading_01) 
     .error(R.drawable.loading_02) 
     .into(holder.imgDefault); 

    } else { 
     holder.imgDefault.setImageDrawable(null); 
    } 

    holder.txtName.setText(menuItems.get(position).getPName()); 
    holder.txtPrice.setText(String.valueOf(menuItems.get(position).getPPrice())); 
    holder.parentLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     prepareBottomSheet(view, position, holder.coordinatorLayout); 
     } 
    }); 
    } 

    @Override 
    public int getItemCount() { 
    if (menuItems.size() > 0) { 

     return menuItems.size(); 

    } else { 
     return 0; 
    } 

    } 

This is an image of my problem

謝謝您的幫助。

+0

你得到什麼錯誤請顯示 –

+0

是一個全球變量的網址?並在加載圖片之前,檢查URL是否爲空。 –

+0

不,我只是從服務器獲取每個項目的Id。我檢查了它們,它們是正確的。 – Ehsan

回答

1

只要把下面用畢加索

Picasso.with(this.context).cancelRequest(holder.imageView); 
    if(imgUrl!=null && imgUrl.trim().length()>0){ 
     //put here picaso image load code 
    }else { 
     holder.imageView.setImageResource(R.drawable.no_img); 
    } 
+0

我做到了,但它不適合我。 – Ehsan

+0

好的,沒問題。只要檢查url是否爲空,然後設置手動繪製,當你找不到圖像url時你想顯示。 –

+0

我用你的代碼和更新我的問題,但它不工作。我不知道我的代碼有什麼問題。 – Ehsan

0

有這樣的圖書館即PICASO和滑翔

但我使用滑行庫,我也沒有什麼問題,在圖像視圖進行負載圖像一行這個問題,但它得到了由下面的代碼解決了,請試試這個代碼...

依賴

compile 'com.github.bumptech.glide:glide:4.0.0' 

    Glide.with(context.getApplicationContext()).load(imageUrl).signature(new StringSignature(String.valueOf(System.currentTimeMillis()))).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView); 
相關問題