如果在RecyclerView中列表为空,我想显示带有空消息的Toast

时间:2017-10-30 08:49:27

标签: java android pagination android-recyclerview android-cardview

我在recyclerView获取了分页概念的默认卡片, 因为片段中的代码如下:

private void onResponse(String response) {


   NewEnquiryPagination newEnquiries = new Gson().fromJson(response, new TypeToken<NewEnquiryPagination>() {
    }.getType());

    Log.i("@sandy,", "newEnquiriesFirstPage: " + newEnquiries);

    if (listOfEnquiries !=null) {

        adapter.addAll(newEnquiries.getContent());

        if (currentPage <= TOTAL_PAGES) adapter.addLoadingFooter();
        else isLastPage = true;
    }
}



private void onResponseNextPage(String response) {

    adapter.removeLoadingFooter();
    isLoading = false;
    NewEnquiryPagination newEnquiries = new Gson().fromJson(response, new TypeToken<NewEnquiryPagination>() {
    }.getType());
    if (listOfEnquiries != null ) {

        adapter.addAll(newEnquiries.getContent());

        if (currentPage != TOTAL_PAGES) adapter.addLoadingFooter();
        else isLastPage = true;
    }
}

我将内容列表显示为空,但默认情况下我已获取卡片设置,并且SwipeRefresh卡片正在增加一张......问题出在loadFirstPage();方法,如果列表为空,我只想将snackbar显示为空,而SwipeRefresh我只想刷新列表。

请帮帮我

1 个答案:

答案 0 :(得分:1)

只需将此If条件设置为检查列表的空值和大小,

 if (newEnquiries.getContent() != null && newEnquiries.getContent().size() > 0) {
        Log.i("@sss","OnRes");
        adapter.addAll(newEnquiries.getContent());
        if (currentPage <= TOTAL_PAGES) adapter.addLoadingFooter();
        else isLastPage = true;
    } else {
      //Your else condition
    }

希望如此有帮助