【问题标题】:How to show data into list in Android如何在Android中将数据显示到列表中
【发布时间】:2017-12-09 11:23:12
【问题描述】:

在我的应用程序中,我想在recyclerView 中显示一些数据,然后我从服务器获取这些数据。
我将Retrofit 用于从 android 到服务器的 sendget 请求。
运行应用程序时,它显示 response.body().getData().size()0,但在 PostMan 中我看到以下数据:

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

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

        }
    });
}

当显示此Toast :Toast.makeText(NewsDetailActivity.this, "" + call.isExecuted(), Toast.LENGTH_SHORT).show(); 时,在toast 中显示true

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

我的适配器代码:

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

        }
    }
}

评论回复代码:

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

评论数据模型:

public class CommentData {
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("text")
    @Expose
    private String text;
    @SerializedName("likeCount")
    @Expose
    private Integer likeCount;
    @SerializedName("repliesCount")
    @Expose
    private Integer repliesCount;
    @SerializedName("submitDate")
    @Expose
    private String submitDate;
    @SerializedName("submitTime")
    @Expose
    private String submitTime;
    @SerializedName("owner")
    @Expose
    private CommentOwner owner;

    public Integer getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(Integer likeCount) {
        this.likeCount = likeCount;
    }

    public Integer getRepliesCount() {
        return repliesCount;
    }

    public void setRepliesCount(Integer repliesCount) {
        this.repliesCount = repliesCount;
    }

    public String getSubmitDate() {
        return submitDate;
    }

    public void setSubmitDate(String submitDate) {
        this.submitDate = submitDate;
    }

    public String getSubmitTime() {
        return submitTime;
    }

    public void setSubmitTime(String submitTime) {
        this.submitTime = submitTime;
    }

    public CommentOwner getOwner() {
        return owner;
    }

    public void setOwner(CommentOwner owner) {
        this.owner = owner;
    }
}

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

【问题讨论】:

  • "运行应用程序时,它显示 response.body().getData().size() 为 0" -- 检查 code()message()errorBody(),可能您收到 HTTP 错误。
  • @CommonsWare,你在这里吗,我的朋友?

标签: android list arraylist android-recyclerview


【解决方案1】:

您似乎以错误的方式向服务器传递了一些数据。您确定 POSTMAN 发送的数据与 Retrofit 发送的数据相同吗?

您能否发布您对api.getComments(sendData) 方法的实现?

【讨论】:

    【解决方案2】:

    这可能有几个原因

    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&lt;CommentData&gt; newContent)方法更新数据。

    【讨论】:

    • 谢谢我的朋友。你有团队查看器吗?请帮帮我
    • 是的,我有。我将在 2 个半小时后上线。那我们应该怎么联系呢?
    • 2.30 小时后在我国很晚。你能把你的 Skype ID 发给我在这里发言吗?请
    • 朋友你在吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多