2015-12-21 60 views
2

我使用從web加載的圖像填充RecyclerView。我可以在我的適配器內使用AsyncTask加載圖像。但是,因爲我需要與畢加索一起實施它,所以我需要幫助。這是迄今爲止代碼:如何使用帶定製適配器的畢加索回收器查看

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieViewHolder> 
{ 
    Bitmap mBitmap; 
    int pos; 
    public static class MovieViewHolder extends RecyclerView.ViewHolder 
    { 
     CardView cv; 
     TextView MovieName; 
     ImageView MoviePhoto; 

     MovieViewHolder(View itemView) { 
      super(itemView); 
      cv = (CardView)itemView.findViewById(R.id.cv); 
      MovieName = (TextView)itemView.findViewById(R.id.movie_name); 
      MoviePhoto = (ImageView)itemView.findViewById(R.id.movie_photo); 
     } 
    } 

    List<Post> mpost; 

    CustomAdapter(List<Post> mpost){ 
     this.mpost = mpost; 
    } 

    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 
    } 

    @Override 
    public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); 
     MovieViewHolder pvh = new MovieViewHolder(v); 
     return pvh; 
    } 

    @Override 
    public void onBindViewHolder(MovieViewHolder movieViewHolder, int i) 
    { 
     pos=i; 
     movieViewHolder.MovieName.setText(mpost.get(i).getTitle()); 

     if(mpost.get(pos).getPoster_path()!=null) 
     { 
      new AsyncTask<Void, Void, Void>() { 
       @Override 
       protected Void doInBackground(Void... params) { 
        try { 
         URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path()); 
         mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

        } catch (MalformedURLException e) { 

        } catch (IOException e) { 

        } 
        return null; 
       } 
      }.execute(); 




      movieViewHolder.MoviePhoto.setImageBitmap(mBitmap); 

     } 




    } 

    @Override 
    public int getItemCount() 
    { 
     if(mpost!=null) 
     { 
      return mpost.size(); 
     } 
     else 
     { 
      return 0; 
     } 
    } 

} 

,我需要更換此:

new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       try { 
        URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path()); 
        mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

       } catch (MalformedURLException e) { 

       } catch (IOException e) { 

       } 
       return null; 
      } 
     }.execute(); 




     movieViewHolder.MoviePhoto.setImageBitmap(mBitmap); 

與畢加索:

Picasso.with(this) 
       .load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()) 
       .into(MoviePhoto); 

然而,似乎有錯誤ddoing所以,請告訴我最好的修復?

+0

... plz檢查我的解決方案...並讓我知道是否有任何顧慮 –

+0

您無法使用'Picasso.with(this)'您需要將您的活動的上下文傳遞給適配器並將其用作'Picasso .with(mContext)'..你可以使用適配器構造函數傳遞活動上下文.. – Droidwala

回答

-1

您可以使用畢加索如下: -

Picasso.with(getContext()).load("your url").into(new Target() { 
      @Override 
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
       //do what ever you want with your bitmap 
       imgView.setImageBitmap(loadedImage);///imgView is use to set the image in it 
      } 

      @Override 
      public void onBitmapFailed(Drawable errorDrawable) { 

      } 

      @Override 
      public void onPrepareLoad(Drawable placeHolderDrawable) { 

      } 
     }); 
+0

謝謝,無法解析符號'getContext()',最新的原因是什麼? :/ – OBX

+0

代替getContex(),您可以將您的活動名稱設置爲「MY_ACYIVITY.THIS」 –

+0

我在發佈的問題中遇到的唯一問題是使用上下文,但無法解決此問題 – OBX

0

在方法onBindViewHolder設置piccaso的圖像加載到圖像視圖

onBindViewHolder(MovieViewHolder movieViewHolder, int position) 
Picasso.with(context) 
      .load("http://image.tmdb.org/t/p/w154" + mpost.get(position).getPoster_path()) 
      .into(movieViewHolder.MoviePhoto);` 

至於這麼簡單。 :)

2
@Override 
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i){ 
    pos=i; 
    movieViewHolder.MovieName.setText(mpost.get(i).getTitle()); 
    if(mpost.get(pos).getPoster_path()!=null){ 
     Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()).into(movieViewHolder.MoviePhoto) 
    } 
} 
1
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieViewHolder> 
{ 
Bitmap mBitmap; 
int pos; 
Context context; 
public static class MovieViewHolder extends RecyclerView.ViewHolder 
{ 
    CardView cv; 
    TextView MovieName; 
    ImageView MoviePhoto; 

    MovieViewHolder(View itemView) { 
     super(itemView); 
     cv = (CardView)itemView.findViewById(R.id.cv); 
     MovieName = (TextView)itemView.findViewById(R.id.movie_name); 
     MoviePhoto = (ImageView)itemView.findViewById(R.id.movie_photo); 
    } 
} 

List<Post> mpost; 

CustomAdapter(Context context,List<Post> mpost){ 
    this.mpost = mpost; 
    this.context=context; 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

@Override 
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); 
    MovieViewHolder pvh = new MovieViewHolder(v); 
    return pvh; 
} 

@Override 
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i) 
{ 
    pos=i; 
    movieViewHolder.MovieName.setText(mpost.get(i).getTitle()); 

    if(mpost.get(pos).getPoster_path()!=null) 
    { 
     new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       try { 
        URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path()); 
        mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

       } catch (MalformedURLException e) { 

       } catch (IOException e) { 

       } 
       return null; 
      } 
     }.execute(); 




     movieViewHolder.MoviePhoto.setImageBitmap(mBitmap); 

    } 




} 

@Override 
public int getItemCount() 
{ 
    if(mpost!=null) 
    { 
     return mpost.size(); 
    } 
    else 
    { 
     return 0; 
    } 
} 

} 

使用此代碼,並通過活動的背景下CustomAdapter。

CustomAdater customAdapter= new CustomAdapter(this,post); 

使用上下文而不是您的畢加索代碼。