显示JSON中的两个不同图像

时间:2018-05-29 06:03:43

标签: android json android-glide

Recycler adapter

package com.example.maztec.celebstars.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.maztec.celebstars.Pojodata.HeaderData;
import com.example.maztec.celebstars.Pojodata.ItemResponse2;
import com.example.maztec.celebstars.Pojodata.actorsrcs;
import com.example.maztec.celebstars.R;

import java.security.PublicKey;
import java.util.List;

public class ImageRecyclerAdapter extends RecyclerView.Adapter<ImageRecyclerAdapter.GetHeadersrcs> {
    private Context context;
    private List<HeaderData> Hsrcs;

    public ImageRecyclerAdapter(Context context, List<HeaderData> hsrcs) {
        this.context = context;
        Hsrcs = hsrcs;
    }

    @NonNull
    @Override
    public ImageRecyclerAdapter.GetHeadersrcs onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(context);
        View v=inflater.inflate(R.layout.single_header_design,parent,false);
        return new GetHeadersrcs(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageRecyclerAdapter.GetHeadersrcs holder, int position) {
        HeaderData hdata=Hsrcs.get(position);
        final String id=hdata.getId();
        Glide.with(context).load(hdata.getCat_image()).error(R.drawable.defaultceleb).into(holder.Headerimg1);
        Glide.with(context).load(hdata.getCat_image()).error(R.drawable.defaultceleb).into(holder.Headerimg2);

    }

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

    public class GetHeadersrcs extends RecyclerView.ViewHolder {
        ImageView Headerimg1;
        ImageView Headerimg2;

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

            Headerimg1=(ImageView) itemView.findViewById(R.id.circ1);
            Headerimg2=(ImageView)itemView.findViewById(R.id.circ2);
        }
    }

    public  void setHeaderdata(List<HeaderData> headerdata){
        this.Hsrcs=headerdata;
        notifyDataSetChanged();
    }
}

enter image description here

这里是JSON响应

{
   "data":[
      {
         "id":"1",
         "cat_name":"Actor",
         "cat_image":"http://mapi.trycatchtech.com/uploads/bollywood_wallpaper/1d96964ff5fee52c459b430399d0ad6b.jpg"
      },
      {
         "id":"2",
         "cat_name":"Actress",
         "cat_image":"http://mapi.trycatchtech.com/uploads/bollywood_wallpaper/7b9d2f40a3f0fe3b6b37e66b5992be3a.jpg"
      }
   ],
   "last_update":"1525853619"
}

我想要的是演员的一种观点和女演员的一种观点,如果你可以指导我做错的地方。

2 个答案:

答案 0 :(得分:0)

我认为你希望每行只有一个图像。所以改变:

HeaderData hdata=Hsrcs.get(position);
final String id=hdata.getId();
Glide.with(context).load(hdata.getCat_image()).error(R.drawable.defaultceleb).into(holder.Headerimg1);
Glide.with(context).load(hdata.getCat_image()).error(R.drawable.defaultceleb).into(holder.Headerimg2);

为:

HeaderData hdata=Hsrcs.get(position);
final String id=hdata.getId();
Glide.with(context).load(hdata.getCat_image()).error(R.drawable.defaultceleb).into(holder.Headerimg1);

您可以删除Headerimg2,因为仅使用Headerimg1即可。

答案 1 :(得分:0)

问题是您正在调用从两个ImageView的JSON数据中检索相同图像的相同方法

hdata.getCat_image()