代码之家  ›  专栏  ›  技术社区  ›  Bo Z

如何将图像链接与改装和安装毕加索联系起来

  •  0
  • Bo Z  · 技术社区  · 6 年前

    需要帮助。我知道如何填充列表。但不知道如何使其与改装+回收视图一起工作。我需要用图像填充回收器视图。图像必须在改进的帮助下提供,从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\u URL) .build(); }

    /**
     * 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;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   androiddev2    6 年前

    我想你没有给我们一个明确的错误或问题,但我会说我之前在改装方面做错了什么。

    1-不要忘记添加 互联网 清单中的权限

    2- 如果您的回答是列表 ,您应该定义 Call<List<YourModelHere>> 而不是

    Call<YourModelHere>
    

    在接口和调用位置。

    3-如果您不知道,请学习在调试模式下工作。因此,基本上您可以理解您正在检索或不检索的响应。

    4-除了这些,我总是 毕加索和;改装 像这样; (但我肯定你错了,因为你应该重新获得职位,你可以得到所有海报,按照你的风格,你只能得到一张或一张)

    Picasso.with(context).load(item.get(position).getPosterPath).into(holder.mImageBanner);
    

    (定义 上下文上下文上下文 在全球范围内)