需要帮助。我知道如何填充List。但不知道如何使用retrofit + recyclerView使其工作。我需要用图像填充回收者视图。图像必须有改进的帮助,从json中提取链接后,必须将它们放在图像视图中。
我的适配器
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);
}
我的模特
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;
}
}
另一个模型,因为里面有一个列表和列表
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;
}
}
我的改装客户
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) 。建立(); }
/**
* Api Service
**/
public static ApiService getApiService(){
return getRetrofitInstance().create(ApiService.class);
}
}
Api服务
public interface ApiService {
@GET("3/movie/popular")
Call<BasePojo> getPhotosList(@Query("api_key") String key);
}
和我的片段
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;
}
答案 0 :(得分:0)
我认为你没有给我们一个明确的错误或问题,但我会在改造之前说出我做错了什么。
1-不要忘记在Manifest中添加 INTERNET 权限
2- 如果您的回复是列表,则应定义
Call<YourModelHere>
代替
Picasso.with(context).load(item.get(position).getPosterPath).into(holder.mImageBanner);
在接口和你打电话的地方。
3-如果您不了解,请学习在调试模式下工作。所以,基本上你可以理解你正在检索哪些回复或者你没有回答哪些回复。
4-除此之外,我总是像这样使用 Picasso&amp; Retrofit ; (但我确定你在这里错了,因为你应该找回位置而且你可以获得所有海报,你做的风格只能得到一个或没有)< / EM> 强>
{{1}}
(在全局中定义上下文上下文)