user9052494
user9052494

Reputation:

How to get a list size?

In my application I want get some list from server and I should first check it.
If notified is false, then add this into my List.
For this I write foreach for read all of list from server.

My Code is :

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

My LogCat message :

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

In logCat show me 5 false message, why in size show me 1?!!!
In size message should show me 5 . not 1

How can I it? please help me

Upvotes: 2

Views: 108

Answers (3)

Omkar
Omkar

Reputation: 3100

add clear() before foreach like below

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

Upvotes: 1

kamal verma
kamal verma

Reputation: 516

When You add seen.add(datum) before you clear seen.clear()

Remove seen.clear()

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

Upvotes: 1

Mureinik
Mureinik

Reputation: 311518

Each time getNotified is false, you clear the seen list, which removes all the elements from it. Just remove that line and you should be OK:

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

Upvotes: 1

Related Questions