gpt4 book ai didi

java - 如何在Android中使用Retrofit在recyclerView中设置数据

转载 作者:行者123 更新时间:2023-12-02 02:44:16 25 4
gpt4 key购买 nike

我想在 recyclerview 中显示一些数据,为此我使用下面显示的代码。

当我运行应用程序时,它没有向我显示 recyclerview 中的任何数据,并且 ListData.size()0,但在 <强>PostMan

我将以下代码写入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) {

}
});

我的适配器代码:

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

}
}
}

如何将数据显示到 recyclerView 中?

最佳答案

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

当您获取响应时,您可以通过

调用它
//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);

这将更新 recyclerView 的内容并通知所做的更改,请尝试此操作,如果您有任何问题,请告诉我。

关于java - 如何在Android中使用Retrofit在recyclerView中设置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com