如何将项目列表添加到我的RecyclerView适配器

时间:2016-11-24 18:03:35

标签: android

在onLoadFinsih我从服务器上回来了我想要的电影

将此列表添加到我的RecyclerView适配器

我尝试创建电影列表并将其传递给我的RecyclerView适配器 然后当我在onLoadFinish方法中从服务器获取电影列表时,我添加 这个列表到我在适配器中传递的列表然后通知适配器 但没有工作

public class MainActivity extends AppCompatActivity
    implements LoaderManager.LoaderCallbacks<List<Movie>>{

        private RecyclerView.Adapter movieRecyclerViewAdapter;
        private RecyclerView.LayoutManager mLayoutManager;
        private List<Movie> mListOfMovies;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    progressBar = (ProgressBar) findViewById(R.id.progress_bar);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mListOfMovies = new ArrayList<>();

    movieRecyclerViewAdapter = new MovieItemAdapter(this, mListOfMovies);
    mLayoutManager = new LinearLayoutManager(this);

    recyclerView.setAdapter(movieRecyclerViewAdapter);

}
@Override
public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> movies) {
    //hide progress bar after fetch data from server
    progressBar.setVisibility(View.GONE);

    mListOfMovies.clear();

    mListOfMovies.addAll(movies);

    movieRecyclerViewAdapter.notifyItemRangeChanged();

}



public class MovieItemAdapter extends RecyclerView.Adapter<MovieItemAdapter.ViewHolder> {

private List<Movie> mListOfMovies;
private Context mContext;

public MovieItemAdapter(Context context, List<Movie> movies) {
    this.mListOfMovies = movies;
    this.mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext)
            .inflate(R.layout.movie_list_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Movie movieItem = mListOfMovies.get(position);

    holder.moviePoster.setImageBitmap(movieItem.getBitmap());
    holder.movieName.setText(movieItem.getTitle());

    DecimalFormat format = new DecimalFormat("0.0");
    holder.voteAverage.setText(format.format(movieItem.getVoteAverage()));
    holder.releaseDate.setText(movieItem.getReleaseDate());
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView moviePoster;
    public TextView movieName;
    public TextView voteAverage;
    public TextView releaseDate;

    public ViewHolder(View itemView) {
        super(itemView);

        moviePoster = (ImageView) itemView.findViewById(R.id.movie_poster);
        movieName = (TextView) itemView.findViewById(R.id.movie_name);
        voteAverage = (TextView) itemView.findViewById(R.id.vote_average);
        releaseDate = (TextView) itemView.findViewById(R.id.release_date);
    }
}

}

2 个答案:

答案 0 :(得分:0)

使适配器类中的ViewHolder类非静态。然后再次检查。

答案 1 :(得分:0)

onLoadFinish()中,您应该使用收到的数据更新适配器中的列表。

然后你应该调用适配器adapter.notifyDataSetChanged()

编辑:

在适配器中创建方法,如

setMovieList(List<Movie> movies){
    mListOfMovies = movies;
    this.notifyDataSetChanged();
} 

并从onLoadFinished()

调用它
movieRecyclerViewAdapter.setMovieList(mListOfMovies)