Need help. I know how to populate List. But dont know how to make it work with retrofit + recyclerView. I need to populate recycler view with images. Images has to come with help of retrofit and after extracting their link from json they has to be put in image view.
My adapter
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
/**
* General
*/
private List<ResultsPojo> resultList;
private LayoutInflater layoutInflater;
/**
* Garbage
*/
private ItemClickListener mClickListener;
public ListAdapter(List<ResultsPojo> resultList, Context context) {
this.resultList = resultList;
this.layoutInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
Context context = holder.mImageBanner.getContext();
ResultsPojo item = resultList.get(position);
Log.d("tag", "on bind view item " + item.toString());
Picasso.get().load(item.getPosterPath()).into(holder.mImageBanner);
}
@Override
public int getItemCount() {
return resultList.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mImageBanner;
public ListViewHolder(View itemView) {
super(itemView);
mImageBanner = (ImageView) itemView.findViewById(R.id.item_banner_image);
itemView.setOnClickListener(this);
}
My model
public class BasePojo {
@SerializedName("page")
@Expose
private int page;
@SerializedName("total_results")
@Expose
private int totalResults;
@SerializedName("total_pages")
@Expose
private int totalPages;
@SerializedName("results")
@Expose
private ArrayList<ResultsPojo> results;
public BasePojo(ArrayList<ResultsPojo> results, int page){
this.results = results;
this.page = page;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalResults() {
return totalResults;
}
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public ArrayList<ResultsPojo> getResults() {
return results;
}
public void setResults(ArrayList<ResultsPojo> results) {
this.results = results;
}
}
Another model because there is a list and list inside it
public class ResultsPojo {
@SerializedName("vote_count")
@Expose
private int voteCount;
@SerializedName("id")
@Expose
private int id;
@SerializedName("video")
@Expose
private boolean video;
@SerializedName("vote_average")
@Expose
private double voteAverage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("popularity")
@Expose
private double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = null;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("adult")
@Expose
private boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
public int getVoteCount() {
return voteCount;
}
public void setVoteCount(int voteCount) {
this.voteCount = voteCount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isVideo() {
return video;
}
public void setVideo(boolean video) {
this.video = video;
}
public double getVoteAverage() {
return voteAverage;
}
public void setVoteAverage(double voteAverage) {
this.voteAverage = voteAverage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPopularity() {
return popularity;
}
public void setPopularity(double popularity) {
this.popularity = popularity;
}
public String getPosterPath() {
return posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public String getOriginalLanguage() {
return originalLanguage;
}
public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
}
public String getOriginalTitle() {
return originalTitle;
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}
public List<Integer> getGenreIds() {
return genreIds;
}
public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}
public String getBackdropPath() {
return backdropPath;
}
public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}
My retrofit client
public class RetrofitClient {
/**
* URLS
**/
private static final String BASE_URL = "http://api.themoviedb.org/";
/**
* Retrofit instance
**/
private static Retrofit getRetrofitInstance(){
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl(BASE_URL) .build(); }
/**
* Api Service
**/
public static ApiService getApiService(){
return getRetrofitInstance().create(ApiService.class);
}
}
Api service
public interface ApiService {
@GET("3/movie/popular")
Call<BasePojo> getPhotosList(@Query("api_key") String key);
}
and my fragment
RecyclerView recyclerView;
View view;
private ArrayList<ResultsPojo> resultsList;
private ListAdapter adapter;
public ListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* Main Initialization
*/
resultsList = new ArrayList<>();
view = inflater.inflate(R.layout.fragment_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_list_detailed);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//adapter.setClickListener(this);
ApiService api = RetrofitClient.getApiService();
Call<BasePojo> call = api.getPhotosList(getString(R.string.api_key));
call.enqueue(new Callback<BasePojo>() {
@Override
public void onResponse(Call<BasePojo> call, Response<BasePojo> response) {
Log.d("tag", "inside on response");
resultsList = response.body().getResults();
adapter = new ListAdapter(resultsList,getContext());
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<BasePojo> call, Throwable t) {
Log.d("tag", "inside failure");
}
});
return view;
}