Как правильно написать onBindViewHolder?

172
03 декабря 2017, 12:50

Как правильно написать onBindViewHolder а то у меня он начинает ругаться на слово cardfeeds

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    CardFeed cardfeed = cardfeeds.get(position);
    holder.cvUser.setText(cardfeed.getName());
    holder.cvLocation.setText(cardfeed.getLocation());
    holder.cvContent.setText(cardfeed.getDescription());
    holder.cvPhoto.setImageResource(cardfeed.getPhoto());
}

Вот полный код адаптера

public class ContentAdapter extends      
RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private List<CardFeed> cardfeed;
public ContentAdapter(List<CardFeed> cardfeed) {
    this.cardfeed = cardfeed;
}

private static final int LENGTH = 18;
public static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView cvUser;
    TextView cvLocation;
    ImageView cvPhoto;
    TextView cvContent;
    public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.card_feed, parent, false));
        cvUser = (TextView) itemView.findViewById(R.id.card_user);
        cvLocation = (TextView) itemView.findViewById(R.id.card_location);
        cvPhoto = (ImageView) itemView.findViewById(R.id.card_image);
        cvContent = (TextView) itemView.findViewById(R.id.card_text);
    }
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    CardFeed cardfeed = cardfeeds.get(position);
    holder.cvUser.setText(cardfeed.getName());
    holder.cvLocation.setText(cardfeed.getLocation());
    holder.cvContent.setText(cardfeed.getDescription());
    holder.cvPhoto.setImageResource(cardfeed.getPhoto());
}
@Override
public int getItemCount() {
    return LENGTH;
}

Вот код листа для адаптера

public class CardFeed {
private String name;
private String location;
private String description;
private int photo;
public CardFeed(){
}
public CardFeed(String name, String location, String description, int photo) {
    this.name = name;
    this.location = location;
    this.description = description;
    this.photo = photo;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getLocation() {
    return location;
}
public void setLocation(String location) {
    this.location = location;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

public int getPhoto() {
    return photo;
}
public void setPhoto(int photo) {
    this.photo = photo;
}
Answer 1

Обзовите ваш список cardfeeds

private List<CardFeed> cardfeeds;
public ContentAdapter(List<CardFeed> cardfeeds) {
    this.cardfeeds = cardfeeds;
}
READ ALSO
Утечка памяти MediaPlayer&#39;a и Media JavaFX&#39;a

Утечка памяти MediaPlayer'a и Media JavaFX'a

Добрый день, написал несложный код

159
Ошибка JS в phpstorm

Ошибка JS в phpstorm

Возникла странная проблема, при написании сайта

191