[英]How to set data in recyclerView with Retrofit in Android
I want to show some data into recyclerview
and for this I am using the code displayed below. 我想向recyclerview
显示一些数据,为此,我正在使用下面显示的代码。
When I run the application, it's not showing me any data into recyclerview
and ListData.size()
is 0 , but in PostMan 当我运行应用程序时,它不会向我显示任何数据到recyclerview
并且ListData.size()
为0 ,但是在PostMan中
I write below code for set into recyclerView
: 我将下面的代码写到recyclerView
:
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CommentResponse> call = api.getComments(sendData);
call.enqueue(new Callback<CommentResponse>() {
@Override
public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
if (response.body().getData() != null) {
commentModel.clear();
commentModel.addAll(response.body().getData());
commentsListAdapter.notifyDataSetChanged();
content_newsCommentsRecyclerView.setAdapter(commentsListAdapter);
}
}
@Override
public void onFailure(Call<CommentResponse> call, Throwable t) {
}
});
My Adapter codes: 我的适配器代码:
public class CommentsListAdapter extends RecyclerView.Adapter<CommentsListAdapter.ViewHolder> {
private Context context;
private List<CommentData> model;
public CommentsListAdapter(Context context, List<CommentData> model) {
this.context = context;
this.model = model;
}
@Override
public CommentsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_comment, parent, false);
return new CommentsListAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final CommentsListAdapter.ViewHolder holder, final int position) {
holder.row_commentNameTxt.setText(Html.fromHtml(model.get(position).getOwner().getName()));
holder.row_commentCommentTxt.setText(Html.fromHtml(model.get(position).getText()));
Glide.with(context)
.load(model.get(position).getOwner().getImageUrl())
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(holder.row_commentProfileImage);
holder.row_commentLikeTxt.setText(model.get(position).getLikeCount() + "");
holder.row_commentReplayTxt.setText(model.get(position).getRepliesCount() + "");
holder.row_commentDateTxt.setText(model.get(position).getSubmitDate() + " " + model.get(position).getSubmitTime());
}
@Override
public int getItemCount() {
return model.size();
}
public void addNewItem(List<CommentData> newContent) {
int start = this.model.size();
int end = newContent.size();
model.addAll(newContent);
notifyDataSetChanged();
}
public void clear() {
model.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView row_commentProfileImage;
private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentReplayTxt, row_commentDateTxt;
public ViewHolder(View itemView) {
super(itemView);
row_commentProfileImage = (CircleImageView) itemView.findViewById(R.id.row_commentProfileImage);
row_commentNameTxt = (TextView) itemView.findViewById(R.id.row_commentNameTxt);
row_commentCommentTxt = (TextView) itemView.findViewById(R.id.row_commentCommentTxt);
row_commentLikeTxt = (TextView) itemView.findViewById(R.id.row_commentLikeTxt);
row_commentReplayTxt = (TextView) itemView.findViewById(R.id.row_commentReplayTxt);
row_commentDateTxt = (TextView) itemView.findViewById(R.id.row_commentDateTxt);
}
}
}
How can I display the data into recyclerView
? 如何将数据显示到recyclerView
?
Create a method inside your Adapter class 在您的Adapter类中创建一个方法
//This will add all the items in the adapter's list
public void addAllItems(List<CommentData> items) {
model.addAll(items);
notifyDataSetChanged();
}
//In Adapter's Constructor do the following changes
public CommentsListAdapter(Context context, List<CommentData> model) {
this.context = context;
this.model = new ArrayList<>;
}
and when you are fetching your response you can call this by 当您获取回复时,可以通过以下方式调用此回复:
//Inside your onCreate add the below code
mAdapter = new CommentsListAdapter (this);
content_newsCommentsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
content_newsCommentsRecyclerView.setAdapter(mAdapter);
//Call this inside your success of onResponse
commentsListAdapter.addAllItems(commentModel);
This will update the content of recyclerView and notify the changes made, do try this and let me know if you have any issue. 这将更新recyclerView的内容并通知所做的更改,请尝试执行此操作,如果有任何问题,请告知我们。
Before the API call initialize the Adapter 在API调用初始化适配器之前
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRVCommentList.setLayoutManager(mLayoutManager);
CommentsListAdapter commentsListAdapter= new CommentsListAdapter(this)
mRVCommentList.setAdapter(commentsListAdapter)
Then call the API 然后调用API
public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
if (response.body().getData() != null) {
List<CommentData> list= response.body().getData().getCommentData();
commentsListAdapter.setData(list);
}
}
Small changes in Adapter Class 适配器类的小变化
private List<CommentData> commentData;
public CommentsListAdapter(Context context) {
this.context = context;
this.commentData = new ArrayList<>();
}
public void setData(List<CommentData> commentData) {
this.commentData= commentData;
notifyDataSetChanged();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.