RecyclerVIew Adapter onBIndVIewHolder中的上下文错误

时间:2017-03-26 21:53:25

标签: android android-layout android-recyclerview android-adapter recycler-adapter

这是我的适配器代码,我在适配器方法onBindViewHolder中遇到错误 无法解析getTitle(),getGenre(),getYear()

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private Context context;
List<Movie> moviesList;
LayoutInflater inflater;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, year, genre;
    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        genre = (TextView) view.findViewById(R.id.genre);
        year = (TextView) view.findViewById(R.id.year);
    }
}

public MoviesAdapter(Context context, List<Movie> movieList) {
     inflater = LayoutInflater.from(context);
    this.moviesList = moviesList;
    this.context = context;
}

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.movie_list_row,parent,false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());//error here as cannot resolve getTitle
    holder.genre.setText(movie.getGenre());//error here as cannot resolve getGenre
    holder.year.setText(movie.getYear());//error here as cannot resolve getYear
}

@Override
public int getItemCount() {
    return moviesList.size();
}


}

这是我的Movie类文件     公共课电影{     私人字符串标题,流派,年份;

public Movie() {
}

public Movie(String title, String genre, String year) {
    this.title = title;
    this.genre = genre;
    this.year = year;
}

public String getTitle() {
    return title;
}

public void setTitle(String name) {
    this.title = name;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}
}

3 个答案:

答案 0 :(得分:0)

问题可能是你的电影有getTitle1()但没有getTitle()看起来:

....
public Movie(String title, String genre, String year) {
    this.title = title;
    this.genre = genre;
    this.year = year;
}

public String getTitle1() {
    return title;
}
...

答案 1 :(得分:0)

也许你已经在你的适配器中导入了android.graphics.Movie而不是你自己的Movie类。 另外,正如@rob所说,你有getTitle1()方法,而不是getTitle()

答案 2 :(得分:0)

你的onBindViewHolder方法可能是错误,而是:

holder.title.setText(movie.getTitle());

尝试:

holder.title.setText(movie.get(position).getTitle());

总体:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.get(position).getTitle());
    holder.genre.setText(movie.get(position).getGenre());
    holder.year.setText(movie.get(position).getYear());
}