|
3 | 3 | import android.content.Context;
|
4 | 4 | import android.content.res.Resources;
|
5 | 5 | import android.graphics.Color;
|
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.v7.widget.RecyclerView; |
6 | 8 | import android.view.LayoutInflater;
|
7 | 9 | import android.view.View;
|
8 | 10 | import android.view.ViewGroup;
|
|
16 | 18 |
|
17 | 19 | import java.util.List;
|
18 | 20 |
|
19 |
| -public class MoviesAdapter extends ArrayAdapter<Movie> { |
| 21 | +public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> { |
20 | 22 |
|
21 |
| - public MoviesAdapter(Context context, List<Movie> movies) { |
22 |
| - super(context, 0, movies); |
| 23 | + private List<Movie> movies; |
| 24 | + |
| 25 | + public class ViewHolder extends RecyclerView.ViewHolder { |
| 26 | + // only needed because we need to set the background color |
| 27 | + View view; |
| 28 | + |
| 29 | + // Lookup view for data population |
| 30 | + TextView tvName; |
| 31 | + TextView tvRating; |
| 32 | + ImageView ivPoster; |
| 33 | + |
| 34 | + public ViewHolder(View itemView) { |
| 35 | + super(itemView); |
| 36 | + |
| 37 | + view = itemView; |
| 38 | + tvName = itemView.findViewById(R.id.tvTitle); |
| 39 | + tvRating = itemView.findViewById(R.id.tvRating); |
| 40 | + ivPoster = itemView.findViewById(R.id.ivPoster); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public MoviesAdapter(List<Movie> movies) { |
| 45 | + this.movies = movies; |
23 | 46 | }
|
24 | 47 |
|
25 | 48 | @Override
|
26 |
| - public View getView(int position, View convertView, ViewGroup parent) { |
27 |
| - // Get the data item for this position |
28 |
| - Movie movie = getItem(position); |
29 |
| - // Check if an existing view is being reused, otherwise inflate the view |
30 |
| - if (convertView == null) { |
31 |
| - convertView = LayoutInflater |
32 |
| - .from(getContext()) |
33 |
| - .inflate(R.layout.item_movie, parent, false); |
34 |
| - } |
35 |
| - // Lookup view for data population |
36 |
| - TextView tvName = (TextView) convertView.findViewById(R.id.tvTitle); |
37 |
| - TextView tvRating = (TextView) convertView.findViewById(R.id.tvRating); |
38 |
| - ImageView ivPoster = (ImageView) convertView.findViewById(R.id.ivPoster); |
| 49 | + public int getItemCount() { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + @NonNull |
| 54 | + @Override |
| 55 | + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 56 | + Context context = parent.getContext(); |
| 57 | + LayoutInflater inflater = LayoutInflater.from(context); |
| 58 | + |
| 59 | + // Inflate the custom layout |
| 60 | + View movieView = inflater.inflate(R.layout.item_movie, parent, false); |
| 61 | + |
| 62 | + // Return a new holder instance |
| 63 | + ViewHolder viewHolder = new ViewHolder(movieView); |
| 64 | + return viewHolder; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onBindViewHolder(MoviesAdapter.ViewHolder viewHolder, int position) { |
39 | 69 |
|
| 70 | + Movie movie = movies.get(position); |
40 | 71 |
|
41 | 72 | // Populate the data into the template view using the data object
|
42 |
| - tvName.setText(movie.getTitle()); |
| 73 | + viewHolder.tvName.setText(movie.getTitle()); |
43 | 74 |
|
44 |
| - Resources resources = getContext().getResources(); |
| 75 | + Resources resources = viewHolder.tvName.getResources(); |
45 | 76 | double movieRating = movie.getRating();
|
46 | 77 |
|
47 | 78 | if (movieRating > 6) {
|
48 |
| - convertView.setBackgroundColor(Color.GREEN); |
| 79 | + viewHolder.view.setBackgroundColor(Color.GREEN); |
49 | 80 | }
|
50 | 81 |
|
51 | 82 | String ratingText = String.format(resources.getString(R.string.rating), movieRating);
|
52 |
| - tvRating.setText(ratingText); |
| 83 | + viewHolder.tvRating.setText(ratingText); |
53 | 84 |
|
54 |
| - Glide.with(getContext()).load(movie.getPosterUrl()).into(ivPoster); |
| 85 | + Glide.with(viewHolder.ivPoster.getContext()).load(movie.getPosterUrl()).into( |
| 86 | + viewHolder.ivPoster); |
55 | 87 |
|
56 |
| - // Return the completed view to render on screen |
57 |
| - return convertView; |
58 | 88 | }
|
59 | 89 | }
|
0 commit comments