使用翻新2在回收者视图上显示项目

时间:2019-02-12 09:39:49

标签: java android json android-recyclerview retrofit2

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方法

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

模型类

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;
    }
}

我的适配器类

 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);

        }

    }

}

主要活动

   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);
            }
        });


    }

我是android Retrofit2 API调用的初学者。 如何获取项目并在回收器视图中设置。我认为没有得到如何将项目设置为适配器类。 这个你能帮我吗。 我已经尝试了所有可能的方法来解决此问题,但是找不到任何解决方案。

1 个答案:

答案 0 :(得分:0)

您的模型有误。它们的配置不正确。请参阅this tutorial,以更好地了解改造和Recyclerview。