RecyclerView错误:没有连接适配器;跳过布局?

时间:2016-05-19 15:36:01

标签: android

我从 TMDB API获取数据。

我正在

  

错误:RecyclerView:没有连接适配器;跳过布局

目前,我正在通过 Retrofit 获取数据。

有人能帮助我吗?

Adapter代码如下:

    public class ReviewAdapter extends RecyclerView.Adapter<MovieDetailActivity.MovieViewHolder> {

    private List<Reviews> rReviewList;
    private LayoutInflater rInflater;
    private Context rContext;

    public ReviewAdapter(Context context) {
        this.rContext = context;
        this.rInflater = LayoutInflater.from(context);
        this.rReviewList = new ArrayList<>();
    }


    public MovieDetailActivity.MovieViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        // create a new view
        View view = rInflater.inflate(R.layout.row_review, parent, false);
        return new MovieDetailActivity.MovieViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MovieDetailActivity.MovieViewHolder holder, int position) {
        Reviews reviews = rReviewList.get(position);
        Picasso.with(rContext)
                .load(reviews.getUrl())

                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Perform click
            }
        });
    }


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

    //To update data
    public void setReviewList(List<Reviews> reviewsList) {
        this.rReviewList = new ArrayList<>();
        this.rReviewList.addAll(reviewsList);
        notifyDataSetChanged();
    }
}

MovieDetailsActivity是我想要获取评论数据的地方

其代码如下:

public class MovieDetailActivity extends AppCompatActivity {
    public static final String EXTRA_MOVIE = "EXTRA_MOVIE";
    private Movie mMovie;
    private Reviews mReviews;
    private Genres mGenres;
    public ReviewAdapter rAdapter;


    //private MoviesAdapter.ReviewAdapter rAdapter;
    ImageView backdrop;
    ImageView poster;
    TextView title;
    TextView description;
    TextView releaseDate;
    TextView voteAverage;
    RecyclerView reviews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);
        if (getIntent().hasExtra(EXTRA_MOVIE)) {
            mMovie = getIntent().getParcelableExtra(EXTRA_MOVIE);
        } else {
            throw new IllegalArgumentException("Detail activity must receive a movie parcelable");
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        toolbarLayout.setTitle(mMovie.getTitle());

        //ucitavanje adaptera

        reviews = (RecyclerView) findViewById(R.id.reviewList);

        reviews.setAdapter(rAdapter);
        rAdapter = new ReviewAdapter(MovieDetailActivity.this);
        getReviews(mMovie.getId());

        backdrop = (ImageView) findViewById(R.id.backdrop);
        title = (TextView) findViewById(R.id.movie_title);
        description = (TextView) findViewById(R.id.movie_description);
        releaseDate = (TextView) findViewById(R.id.movie_date);
        voteAverage = (TextView) findViewById(R.id.movie_rating);
        //genres = (ListView) findViewById(R.id.genresArray);


        title.setText(mMovie.getTitle());
        description.setText(mMovie.getDescription());
        voteAverage.setText(mMovie.getVoteAverage());
        releaseDate.setText(mMovie.getReleaseDate());


        poster = (ImageView) findViewById(R.id.movie_poster);
        String internetUrl = "http://image.tmdb.org/t/p/w500";

        Glide.with(this)
                .load(mMovie.getPoster())
                .override(800, 890)
                .into(poster);

        Glide.with(this)
                .load(mMovie.getBackdrop())
                .into(backdrop);
    }


    private void getReviews(String id) {
        RestAdapter.getMovieService().getReviews(id, new Callback<ReviewWraper>() {
            @Override
            public void success(ReviewWraper reviewWraper, Response response) {
                rAdapter.setReviewList(reviewWraper.getResults());
            }

            @Override
            public void failure(RetrofitError error) {
                error.printStackTrace();

            }
        });
    }

    public static class MovieViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public MovieViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.picture_review);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

添加/编辑此行

reviews = (RecyclerView) findViewById(R.id.reviewList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        reviews.setLayoutManager(mLayoutManager);
      rAdapter = new ReviewAdapter(MovieDetailActivity.this);
        reviews.setAdapter(rAdapter);

答案 1 :(得分:0)

您必须创建适配器,使用此:

rAdapter = new ReviewAdapter(MovieDetailActivity.this);
reviews.setAdapter(rAdapter);

相反

reviews.setAdapter(rAdapter);
    rAdapter = new ReviewAdapter(MovieDetailActivity.this);

请记住,您的Recycler视图需要LayoutManager,如下所示:

reviews.setLayoutManager(
    new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));