从JSON数据清空RecyclerView

时间:2017-11-17 17:37:07

标签: java android json android-recyclerview

感谢迄今为止的所有帮助。这对我来说有点奇怪。我正在建立网络请求以从服务器获取数据并使用json字符串更新UI。我已经正确地完成了json解析并将结果打印在我的日志中,如下所示:

<Calendar>

我使用了一个模型类来保存评论并从recyclerview程序创建了适配器。我的模型类包含来自服务器的评论数据的名称,图像,时间和正文。

这是我的适配器代码:

11-17 22:57:02.143 25111-25111/com.billionusers.tingting D/MyReviewsFragment: FAN Lib My Reviews Response is:   {"success":true,"reviews":[{"_id":"5a0ef724b004b53e48eafaf6","updatedAt":"2017-11-17T14:50:12.700Z","createdAt":"2017-11-17T14:50:12.700Z","for":"5a0d90b8f690f625df370bb6","by":"5a0aa8f7aecfbc47838fb89b","body":"Lorem Ipsum","__v":0}]}

在我的片段中,我初始化了视图和字段,如:

public class ReviewsAdapter extends RecyclerView.Adapter<MyReviewsHolder> {

private final Context context;
private List<Reviews> itemsList;

public ReviewsAdapter(Context context, List<Reviews> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public MyReviewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_reviews_item_layout, parent, false);
    return new MyReviewsHolder(view);
}

@Override
public void onBindViewHolder(MyReviewsHolder viewholder, int position) {
    Reviews reviews = itemsList.get(position);
    Picasso.with(context)
            .load(reviews.getReviewerImg())
            .into(viewholder.reviewerProfileImg);
    viewholder.reviewerNameTV.setText(reviews.getReviewerName());
    viewholder.reviewTimeTV.setText(reviews.getReviewTime());
    viewholder.reviewBodyTV.setText(reviews.getReviewBody());
}

@Override
public int getItemCount() {
    if (itemsList == null) {
        return 0;
    }
    return itemsList.size();
}
}

发出网络请求的代码。我还在这里完成了json解析,并尝试使用数据更新UI,但recyclelerview显示空状态。

这是网络和用户界面代码:

private TextView dummy;
private RecyclerView my_reviews_recyclerView;

private List<Reviews> itemsList = new ArrayList<>();
private ReviewsAdapter adapter;

private boolean success;

public MyReviewsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_my_reviews, container, false);

    dummy = view.findViewById(R.id.dummy);

    my_reviews_recyclerView = view.findViewById(R.id.my_reviews_recyclerView);
    my_reviews_recyclerView.setHasFixedSize(true);
    my_reviews_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    fetchMyReviews();

    return view;
}

有人可以指出我错过了什么,因为我不知道问题出在哪里。感谢。

2 个答案:

答案 0 :(得分:0)

首先尝试进行问题排查,

  1. 检查您获取的json是否为空
  2. 从那里,您现在可以检查您正在提取的json是问题还是适配器。

答案 1 :(得分:0)

这是错误:

JSONObject object = response.getJSONObject("");
                        success = (boolean) object.get("success");
                        JSONArray reviewsArray = object.getJSONArray("reviews");

为什么你从响应中使用空的jsonobject?直接使用:

                    success = (boolean) response.get("success");
                    JSONArray reviewsArray = response.getJSONArray("reviews");