如何获取列表大小?

时间:2017-12-30 07:59:36

标签: java android list

在我的应用程序中,我想从服务器获取一些列表,我应该首先检查它 如果已通知 错误,则添加到我的列表中。 为此,我写了 foreach ,以便从服务器读取所有 list

我的代码是:

private void getNotificationCountData() {
    ExploreSendData sendData = new ExploreSendData();
    sendData.setPageIndex(1);
    sendData.setPageSize(10);
    sendData.setShowFollows(true);
    sendData.setShowMovies(false);
    sendData.setShowNews(false);
    sendData.setShowReplies(true);
    sendData.setShowSeries(false);
    sendData.setShowSuggestions(true);

    InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
    Call<ExploreResponse> call = api.getExplore(token, sendData);

    call.enqueue(new Callback<ExploreResponse>() {
        @Override
        public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
            if (response.body().getData() != null && response.body().getStatusCode() != 401
                    && response.body().getStatusCode() != 402) {
                if (response.body().getData().size() > 0) {
                            seen.clear();
                    for (Datum datum : response.body().getData()) {
                        //Set not seen
                        if (!datum.getNotified()) {

                            seen.add(datum);
                            Log.e("Notification", "MAIN notified : " + datum.getNotified());
                        }

                        if (!datum.getNotified()) {
                            // Badges
                            AHNotification notification = new AHNotification.Builder()
                                    .setText(seen.size() + "")
                                    .setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent))
                                    .setTextColor(ContextCompat.getColor(context, R.color.cardview_light_background))
                                    .build();
                            mainBottomNav.setNotification(notification, 3);
                        }
                    }
                    Log.e("Notification", "Seen Size : " + seen.size());
                }
            } else {
                prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
                startActivity(new Intent(context, LoginActivity.class));
            }

        }

        @Override
        public void onFailure(Call<ExploreResponse> call, Throwable t) {
        }
    });
}

我的LogCat消息:

12-30 11:19:23.866 1507-1507/com.test.app E/Notification: MAIN notified : false
12-30 11:19:23.868 1507-1507/com.test.app E/Notification: MAIN notified : false
12-30 11:19:23.868 1507-1507/com.test.app E/Notification: MAIN notified : false
12-30 11:19:23.869 1507-1507/com.test.app E/Notification: MAIN notified : false
12-30 11:19:23.869 1507-1507/com.test.app E/Notification: MAIN notified : false
12-30 11:19:23.869 1507-1507/com.test.app E/Notification: Seen Size : 1

logCat向我显示 5次消息,为什么在尺寸上显示 1 ?!!!
大小消息应该显示5。不是1

我该怎么办?请帮帮我

3 个答案:

答案 0 :(得分:1)

每次getNotifiedfalse时,您都会清除seen列表,该列表会删除其中的所有元素。只需删除该行,您就可以了:

if (!datum.getNotified()) {
    // seen.clear(); // This line should be removed
    seen.add(datum);
    Log.e("Notification", "MAIN notified : " + datum.getNotified());
}

答案 1 :(得分:1)

在清除seen.add(datum)

之前添加seen.clear()

删除seen.clear()

 if (!datum.getNotified()) {
                        seen.clear();
                        seen.add(datum);
                        Log.e("Notification", "MAIN notified : " + datum.getNotified());
                    }

答案 2 :(得分:1)

clear()之前添加foreach,如下所示

if (response.body().getData().size() > 0) {
                    seen.clear();// change here
                    for (Datum datum : response.body().getData()) {
                        //Set not seen
                        if (!datum.getNotified()) {

                            seen.add(datum);
                            Log.e("Notification", "MAIN notified : " + datum.getNotified());
                    }