picassoライブラリを使用してurlをimageViewに読み込もうとしcontext
ていますが、picassoライブラリを正しく使用できません。
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private List<Post> mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public ImageView pub_image;
public ViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstline);
pub_image = (ImageView) v.findViewById(R.id.imageView);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public FeedAdapter(List<Post> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public FeedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feedholder, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.txtHeader.setText(mDataset.get(position).getPost_text());
Picasso.with(this.context).load("http://i.imgur.com/DvpvklR.png").into(holder.pub_image);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
ここにはいくつかのオプションがあります。
Context
FeedAdapterの引数として、クラスフィールドとしてそれを維持Context
して、必要なときに注入します。それについて読むことを強く勧めます。そのための素晴らしいツールがあります-Dagger by Square任意のView
オブジェクトから取得します。あなたの場合、これはあなたのために働くかもしれません:
holder.pub_image.getContext()
そのままpub_image
ですImageView
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加