Kmh54 Kmh54 - 2 months ago 13
Android Question

How to load data into RecyclerView with Retrofit2 inAndroid

I want load data from server and show into

recyclerView
, for connection i use
Retrofit2
.

First i load categoryData and it's ok, i can see categoryData. but when click on category post i want load this post data into other activity but when go to this
activity
NOT show data!


I pass postID for for load this post and set this postID in
URL
.


With this code, i pass postID from categoryAdapter into
activity
with this code:

((DataViewHolder) holder).main_post_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getPosition();
R_CatModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), PostShow_page.class)
.putExtra("postID", model.getId())


postShowActivity
retrofit
ApiInterface :

@GET("api/get_post")
Call<R_PostModelResponse> getPostResponse(@Query("post_id") Integer id);


postShowActivity codes:

private void bindData() {

// Setup Connect
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<R_PostModelResponse> call = apiInterface.getPostResponse(postID);

call.enqueue(new Callback<R_PostModelResponse>() {
@Override
public void onResponse(Call<R_PostModelResponse> call, Response<R_PostModelResponse> response) {

if (response != null) {

models = response.body().getPost();

mAdaper = new CommentAdapter2(context, models);
Toast.makeText(PostShow_page.this, "GoTo Adapter", Toast.LENGTH_SHORT).show();
comment_Recyclerview.setAdapter(mAdaper);

}
}

@Override
public void onFailure(Call<R_PostModelResponse> call, Throwable t) {

}
});
}


postShowAdapter code:

public class CommentAdapter2 extends RecyclerView.Adapter<CommentAdapter2.ViewHolder> {

private List<R_PostModel> mDataSet;
private Context context;

public CommentAdapter2(Context context,List<R_PostModel> mDataSet) {
this.mDataSet = mDataSet;
this.context = context;
}

@Override
public CommentAdapter2.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment_layout, viewGroup, false);
return new ViewHolder(view);
}

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

viewHolder.comment_name.setText(mDataSet.get(position).getComments().get(1).getCmName());
viewHolder.comment_date.setText(mDataSet.get(position).getComments().get(4).getCmDate());
viewHolder.comment_content.setText(mDataSet.get(position).getComments().get(3).getCmContent());


}

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

public class ViewHolder extends RecyclerView.ViewHolder{

private TextView comment_name, comment_content, comment_date;

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

comment_name = (TextView) itemView.findViewById(R.id.comment_userName_text);
comment_content = (TextView) itemView.findViewById(R.id.comment_comment_text);
comment_date = (TextView) itemView.findViewById(R.id.comment_date_text);

}
}

}


postDataModel:

public class R_PostModel {

@SerializedName("id")
public Integer id;
@SerializedName("type")
public String type;
@SerializedName("slug")
public String slug;
@SerializedName("url")
public String url;
@SerializedName("status")
public String status;
@SerializedName("title")
public String title;
@SerializedName("title_plain")
public String title_plain;
@SerializedName("content")
public String content;
@SerializedName("excerpt")
public String excerpt;
@SerializedName("date")
public String date;
@SerializedName("modified")
public String modified;
@SerializedName("comment_count")
public int comment_count;
@SerializedName("comment_status")
public String comment_status;
@SerializedName("comments")
public List<R_PostComment> comments;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getStatus() {
return status;
}

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

public String getTitle() {
return title;
}

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

public String getTitle_plain() {
return title_plain;
}

public void setTitle_plain(String title_plain) {
this.title_plain = title_plain;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getExcerpt() {
return excerpt;
}

public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getModified() {
return modified;
}

public void setModified(String modified) {
this.modified = modified;
}

public int getComment_count() {
return comment_count;
}

public void setComment_count(int comment_count) {
this.comment_count = comment_count;
}

public String getComment_status() {
return comment_status;
}

public void setComment_status(String comment_status) {
this.comment_status = comment_status;
}

public List<R_PostComment> getComments() {
return comments;
}

public void setComments(List<R_PostComment> comments) {
this.comments = comments;
}


postShowModelResponse:

public class R_PostModelResponse {

@SerializedName("status")
public String status;
@SerializedName("post")
public List<R_PostModel> post;

public String getStatus() {
return status;
}

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

public List<R_PostModel> getPost() {
return post;
}

public void setPost(List<R_PostModel> post) {
this.post = post;
}
}


Same json:

{
"status": "ok",
"post": {
"id": 2289,
"type": "post",
"slug": "11",
"url": "http:\/\/site.com\/1393\/02\/12\/11\/",
"status": "publish",
"title": "Donec tempus urna risus",
"title_plain": "Donec tempus urna risus",
"content": "<p class=\"big\">Lorem ipsum dolor sit amet neque vitae mauris. Etiam malesuada ultricies. Nullam ut nunc odio eget volutpat a, rutrum ac, magna. Nulla facilisi. Nullam justo. Sed leo tristique senectus et ultrices sit amet, consectetuer adipiscing ornare. Nullam vulputate luctus. Nulla interdum libero. Maecenas tincidunt. Pellentesque dolor. In urna. Suspendisse sollicitudin. Vestibulum tempus purus fermentum imperdiet tincidunt, risus pede, luctus laoreet. Aenean ac eros quis eleifend congue. Nam dolor eget velit. Suspendisse at<\/p>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"35\">35<\/span><\/div><h3 class=\"title\">countries<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"142\">142<\/span><\/div><h3 class=\"title\">articles<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"89\">89<\/span><\/div><h3 class=\"title\">projects<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"hr_dots\" style=\"margin: 0 auto 30px;\"><span><\/span><span><\/span><span><\/span><\/div>\n\n<p>Praesent odio ac turpis luctus eu, ornare varius, leo. Suspendisse sed metus. Class aptent taciti sociosqu ad litora torquent per inceptos hymenaeos. Sed sed est. Sed venenatis. Morbi tincidunt. Nullam justo. Vestibulum ut justo a odio. Etiam et pede eget metus nonummy at, rhoncus dolor in interdum pellentesque quis, lacinia aliquet. In mauris sit amet leo. Aliquam erat consectetuer vestibulum varius. Cras ut orci at lorem odio tellus hendrerit sed, congue fringilla.<\/p>\n<p>Class aptent taciti sociosqu ad litora torquent per inceptos hymenaeos. Maecenas vehicula, dui nulla, egestas sodales, augue commodo nec, ullamcorper ligula lorem odio sit amet, vestibulum wisi ultricies eu, magna. Fusce aliquet elit, gravida tellus tristique eget, porta eget, eros. In hac habitasse platea dictumst. Proin cursus magna. Sed placerat. Mauris vel risus. Nunc accumsan dictum, laoreet viverra. Cras tempus purus at risus auctor tincidunt. Sed sed tortor. In venenatis consequat. Donec iaculis. Curabitur est pretium wisi, sed leo. Sed tincidunt risus neque ultrices consectetuer. Etiam mollis ut, dolor. Maecenas.<\/p>\n",
"excerpt": "<p>Donec tempus, urna risus nec mauris. Lorem ipsum primis in nulla ac arcu vitae augue. Vivamus nec elit a dui. Morbi id leo nec dui. Maecenas.<\/p>\n",
"date": "\u06f1\u06f3\u06f9\u06f3-\u06f0\u06f2-\u06f1\u06f2 \u06f0\u06f9:\u06f3\u06f8:\u06f3\u06f1",
"modified": "2014-05-02 09:38:31",
"comments": [{
"id": 10,
"name": "&#1605;&#1581;&#1605;&#1583;",
"url": "",
"date": "2016-09-28 00:00:00",
"content": "<p>&#1578;&#1587;&#1578; &#1662;&#1610;&#1575;&#1605; &#1575;&#1586; &#1587;&#1605;&#1578; &#1576;&#1585;&#1606;&#1575;&#1605;&#1607; &#1606;&#1608;&#1610;&#1587;&#1610;<\/p>\n",
"parent": 0
}, {
"id": 11,
"name": "Mohammad1",
"url": "",
"date": "2016-09-28 00:00:00",
"content": "<p>Test Comment 1<\/p>\n",
"parent": 0
}, {


For test i insert
Toast
message in postShowActivity
retrofit
response, but not show this
Toast
!

Toast.makeText(PostShow_page.this, "GoTo Adapter", Toast.LENGTH_SHORT).show();


Update :
When running application, not calling onResponse, calling onFailure!

How can i fix this problem and show data?

Answer

Please try to change your R_PostModelResponse by this :

public class R_PostModelResponse {

    @SerializedName("status")
    public String status;
    @SerializedName("post")
    public R_PostModel post;

    public String getStatus() {
        return status;
    }

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

    public R_PostModel getPost() {
        return post;
    }

    public void setPost(R_PostModel post) {
        this.post = post;
    }
}

Now, please change your CommentAdapter2 class by this :

public class CommentAdapter2 extends RecyclerView.Adapter<CommentAdapter2.ViewHolder> {

    private R_PostModel mDataSet;
    private Context context;

    public CommentAdapter2(Context context,R_PostModel mDataSet) {
        this.mDataSet = mDataSet;
        this.context = context;
    }

    @Override
    public CommentAdapter2.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment_layout, viewGroup, false);
        return new ViewHolder(view);
    }

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

        viewHolder.comment_name.setText(mDataSet.getComments().get(1).getCmName());
        viewHolder.comment_date.setText(mDataSet.getComments().get(4).getCmDate());
        viewHolder.comment_content.setText(mDataSet.getComments().get(3).getCmContent());


    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView comment_name, comment_content, comment_date;

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

            comment_name = (TextView) itemView.findViewById(R.id.comment_userName_text);
            comment_content = (TextView) itemView.findViewById(R.id.comment_comment_text);
            comment_date = (TextView) itemView.findViewById(R.id.comment_date_text);

        }
    }

}
Comments