如何在Android中将数据显示到列表中

时间:2017-07-05 17:07:40

标签: android list arraylist android-recyclerview

在我的应用程序中,我希望在RestfulController中显示一些数据,并从服务器获取此数据。
我使用recyclerView发送获取请求从android到服务器。
运行应用程序时,它显示Retrofit 0 ,但在 PostMan 中,我看到以下数据:

我在下面写了获取数据的代码:

response.body().getData().size()

显示此private void getComments() { CommentSendData sendData = new CommentSendData(); sendData.setEntityID(7); sendData.setReviewType(5); sendData.setReviewUserType(0); sendData.setEntityID(newsID); sendData.setCelebrityID(0); sendData.setPageIndex(0); sendData.setPageSize(10); 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) { Toast.makeText(NewsDetailActivity.this, "" + response.body().getData().size(), Toast.LENGTH_SHORT).show(); Toast.makeText(NewsDetailActivity.this, "" + call.isExecuted(), Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<CommentResponse> call, Throwable t) { } }); } 时:ToastToast.makeText(NewsDetailActivity.this, "" + call.isExecuted(), Toast.LENGTH_SHORT).show();中向我显示 true

当显示此toastToast时,请在Toast.makeText(NewsDetailActivity.this, "" + response.body().getData().size(), Toast.LENGTH_SHORT).show();中显示 0

我的适配器代码:

toast

CommentResponse代码:

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

        }
    }
}

CommentData模型:

public class CommentResponse {
    @SerializedName("statusCode")
    @Expose
    private Integer statusCode;
    @SerializedName("statusMessage")
    @Expose
    private String statusMessage;
    @SerializedName("data")
    @Expose
    private List<CommentData> data = null;

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<CommentData> getData() {
        return data;
    }

    public void setData(List<CommentData> data) {
        this.data = data;
    }
}

为什么在运行应用程序时显示 0 数据但在 PostMan 显示数据?

2 个答案:

答案 0 :(得分:1)

可能有几个原因

1。)键入基本URL或辅助URL时可能会出现一些错误。请交叉检查。并检查“/”在基本URL之后出现的另一个问题,因此不需要在辅助URL之前使用它。

2.请交叉检查有效载荷,因为它们必须准确无误。如果您不知道如何创建准确的有效负载,请遵循此spinet。

private JsonObject makeJsonObjectPayload() {

        JsonObject requestBean = new JsonObject();
    requestBean.addProperty("key", value);
    requestBean.addProperty("key", value);
    requestBean.addProperty("key", value);
    requestBean.addProperty("key", value);
    requestBean.addProperty("key", value);

    return requestBean;
}

3.如果您没有注意到您没有更新回收站视图中的数据。因此,请使用addNewItem(List<CommentData> newContent)方法更新数据。

答案 1 :(得分:0)

看起来你正在以错误的方式将一些数据传递给服务器。您确定从POSTMAN发送的数据与从Retrofit发送的数据相同吗?

您可以发布api.getComments(sendData)方法的实现吗?