Display items on recycler view using retrofit 2

问题: API Json { "status": true, "message": "Subjects found.", "data": { "subjects": [ { "subj_id": "2", "name":...

问题:

API Json

{
    "status": true,
    "message": "Subjects found.",
    "data": {
        "subjects": [
            {
                "subj_id": "2",
                "name": "Maths",
                "img": "Math.jpg"
            },
            {
                "subj_id": "1",
                "name": "Physics",
                "img": "physics.png"
            }
        ],
        "total": 2
    }
}

GET Method

@GET(WebServices.GET_ACTIVE_SUBJECT)
    Call<SubjectTopics> getSubjects();

Model Class

public class SubjectTopics
{
    @SerializedName("status")
    @Expose
    private Boolean status;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private Data data;

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

 @SerializedName("subjects")
    @Expose
    private List<Subjects> subjects = null;
    @SerializedName("total")
    @Expose
    private Integer total;

    public List<Subjects> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subjects> subjects) {
        this.subjects = subjects;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

public class Subjects {
    @SerializedName("subj_id")
    @Expose
    private String subjId;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("img")
    @Expose
    private String img;

    public String getSubjId() {
        return subjId;
    }

    public void setSubjId(String subjId) {
        this.subjId = subjId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }
}

My Adapter Class

 public class DataAdapter extend 
    RecyclerView.Adapter<DataAdapter.ViewHolder> {

    private ArrayList<Subjects> android;
    private Context context;

    public DataAdapter(ArrayList<Subjects> android,Context context) {
        this.android = android;
        this.context = context;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.subject_topic_list_row,
                viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int position) {

        viewHolder.subjectName.setText(android.get(position).getName());

        viewHolder.relativeClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(context, SubjectTopicList.class);

                intent.putExtra("subject_id", android.get(position).getSubjId());

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }

        });

        Picasso.with(context)
                .load(android.get(position).getImg())
                .placeholder(R.drawable.load)
                .into(viewHolder.ImageV);    
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView subjectName;
        private TextView ID;
        private ImageView ImageV;
        private RelativeLayout relativeClick;

        public ViewHolder(View view) {
            super(view);

            subjectName = (TextView) itemView.findViewById(R.id.textView);
            relativeClick = (RelativeLayout) itemView.findViewById(R.id.relative_click);
            ImageV = (ImageView) itemView.findViewById(R.id.imageView);

        }

    }

}

Main Activity

   private void initViews() {

        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(UnitTestSubjects.this);
        recyclerView.setLayoutManager(layoutManager);



        if (NetworkUtils.isNetworkAvailableToastIfNot(getApplicationContext())) {
      getSubjects();

        }
    }

private void getSubjects() {

        progressBar.setVisibility(View.VISIBLE);

        Call<SubjectTopics> getProductsModelClassCall = webService.getSubjects();

        getProductsModelClassCall.enqueue(new Callback<SubjectTopics>() {
            @Override
            public void onResponse(Call<SubjectTopics> call, Response<Example> response) {
                if (response.isSuccessful()) {


                    SubjectTopics jsonResponse = response.body();
                list = new ArrayList<Subjects>(jsonResponse.getData().getSubjects());
                adapter = new DataAdapter(list);
                recyclerView.setAdapter(adapter);




                } else {
                    APIError apiError = ErrorUtils.parseError(response);

                    Toast.makeText(UnitTestSubjects.this, ""+apiError, Toast.LENGTH_SHORT).show();

                }

                if (progressBar.isEnabled())
                    progressBar.setVisibility(View.INVISIBLE);
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<Example> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(UnitTestSubjects.this, "Please Try Again", Toast.LENGTH_SHORT).show();
                if (progressBar.isEnabled())
                    progressBar.setVisibility(View.INVISIBLE);
                progressBar.setVisibility(View.GONE);
            }
        });


    }

I am beginner in android Retrofit2 API call. How to fetch items and set in recycler view .I think am not getting how to set items to the adapter class. please help me out with this. I have tried all possible ways to solve but not able to find any solution regarding this.


回答1:

You have error with your models. They aren't properly configured. Please see this tutorial for a better understanding of retrofit and recyclerview.

  • 发表于 2019-02-13 18:58
  • 阅读 ( 161 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除