Запрос Gson в RecyclerView - PullRequest
0 голосов
/ 13 ноября 2018

Я хочу установить ответные данные для макета. Я добился успеха, чтобы получить ответ. Как я могу установить данные ответа для определенных полей в программе повторного просмотра? Как я могу установить container_id, заголовок, описание, тег? эти поля находятся внутри в классе модели результата. класс результата находится внутри класса учебной модели. Как я могу найти и установить макет.

Класс фрагмента запроса:

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ArrayList<Study> todoArrayList = new ArrayList<Study>();

    textView = view.findViewById(R.id.txt_book_name);

    Map<String, String> header = new HashMap<>();
    header.put("Content-Type", "application/x-www-form-urlencoded");

    Map<String, String> params = new HashMap<>();
    params.put("fire", "study-material");
    params.put("job_id", "317");

    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getContext()));

    GsonRequest<Study> gsonRequest = new GsonRequest<>(Request.Method.POST, url, Study.class, header, params,
            new Response.Listener<Study>() {
                @Override
                public void onResponse(Study response) {

List resultList = new ArrayList <> ();

                    Result result = new Result();

                    result.setContent_id(result.getContent_id());
                    result.setTitle(result.getTitle());

                    resultList.add(result);

                    todoArrayList.add(new Study(response.getMessage(),resultList,response.getStatus()));


                    Log.d(TAG, response);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Log.d(TAG,error.toString());
        }
    });

    requestQueue.add(gsonRequest);

    final AdapterForStudy itemsAdapter = new AdapterForStudy(TodoFragment.this.getActivity(), todoArrayList);
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycle);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(TodoFragment.this.getActivity(), 2);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(itemsAdapter);
}

Адаптер для ресайклераПросмотреть:

public class AdapterForStudy extends RecyclerView.Adapter<AdapterForStudy.RecyclerVH> {
private Context mCtx;
private List<Study> inProgressList;

public AdapterForStudy(Context mCtx, List<Study> inProgressList) {
    this.mCtx = mCtx;
    this.inProgressList = inProgressList;
}

@NonNull
@Override
public RecyclerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(mCtx);
    View view = layoutInflater.inflate(R.layout.list_todo, parent, false);
    return new AdapterForStudy.RecyclerVH(view);
}


@Override
public void onBindViewHolder(@NonNull final RecyclerVH holder, int position) {

    final Study study = inProgressList.get(position);

    holder.imgDownload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.imgDownload.setImageResource(R.drawable.ic_file_download_green_24dp);
        }
    });

    holder.imgBookmark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.imgBookmark.setImageResource(R.drawable.ic_bookmark_green_24dp);
        }
    });
}

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

public class RecyclerVH extends RecyclerView.ViewHolder {
    TextView txtBook, txtAuthor, txtPage;
    ImageButton imgDownload, imgBookmark;

    public RecyclerVH(@NonNull View itemView) {
        super(itemView);
        txtBook = itemView.findViewById(R.id.txt_book_name);
        txtAuthor = itemView.findViewById(R.id.txt_author);
        txtPage = itemView.findViewById(R.id.txt_pages);
        imgDownload = itemView.findViewById(R.id.img_download);
        imgBookmark = itemView.findViewById(R.id.img_bookmark);
    }
}

}

Модель Классы:

public class Study {
private String message;

private List<Result> result;

private String status;

public Study(String message, List<Result> result, String status) {
    this.message = message;
    this.result = result;
    this.status = status;
}

public String getMessage() {
    return message;
}

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

public List<Result> getResult() {
    return result;
}

public void setResult(List<Result> result) {
    this.result = result;
}

public String getStatus() {
    return status;
}

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

}

public class Result {
private String tags;

private List<Study_materials> study_materials;

private String title;

private String content_id;

private String description;

public String getTags() {
    return tags;
}

public void setTags(String tags) {
    this.tags = tags;
}

public List<Study_materials> getStudy_materials() {
    return study_materials;
}

public void setStudy_materials(List<Study_materials> study_materials) {
    this.study_materials = study_materials;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent_id() {
    return content_id;
}

public void setContent_id(String content_id) {
    this.content_id = content_id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

}

public class Study_materials {
private String study_material_id;

private String material_type;

private String study_material;

public String getStudy_material_id() {
    return study_material_id;
}

public void setStudy_material_id(String study_material_id) {
    this.study_material_id = study_material_id;
}

public String getMaterial_type() {
    return material_type;
}

public void setMaterial_type(String material_type) {
    this.material_type = material_type;
}

public String getStudy_material() {
    return study_material;
}

public void setStudy_material(String study_material) {
    this.study_material = study_material;
}

}

1 Ответ

0 голосов
/ 13 ноября 2018
public void onBindViewHolder(@NonNull final RecyclerVH holder, int position) {

    final Study study = inProgressList.get(position);
    holder.textbook.setText = study.getMessage();

}
...