简体   繁体   English

数据未添加到list.add

[英]Data is not added to list.add

Data is not added to List. 数据未添加到列表。 The program gets data from a GraphQL server and filter it through a switch statements, then appends it to the corresponding list. 该程序从GraphQL服务器获取数据,并通过switch语句对其进行过滤,然后将其附加到相应的列表中。 However, the list is empty if accessed outside the switch statement. 但是,如果在switch语句外部访问,则该列表为空。 If you print one of the list in the switch statement, it will print correctly, however if you print it in the getter functions, it will not return anything. 如果在switch语句中打印列表之一,则将正确打印,但是如果在getter函数中打印,则不会返回任何内容。

What is wrong with it? 怎么了 The scope? 范围? I tried putting the initialization in a few places like on the same function or on the constructor however the result is the same. 我尝试将初始化放在相同功能或构造函数的几个位置,但结果相同。

package sukebei.anilog.data.source.AniList;

import android.util.Log;

import com.apollographql.apollo.ApolloCall;
import com.apollographql.apollo.ApolloClient;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.exception.ApolloException;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;

import AniList.UserMediaQuery;
import AniList.UserMediaQuery.Data;
import AniList.type.MediaType;

/*  Data Received Example

[
    Entry {
        __typename = MediaList, 
        progress = 0, 
        progressVolumes = null, 
        score = 0.0, 
        status = PLANNING, 
        notes = null, 
        repeat = 0, 
        media = Media {
            __typename = Media, 
            chapters = null, 
            volumes = null, 
            idMal = 17082, 
            episodes = 12, 
            title = Title {
                __typename = MediaTitle, romaji = Aiura
            }
        }
    }, 
    Entry {
        __typename = MediaList, 
        progress = 0, 
        progressVolumes = null, 
        score = 0.0, 
        status = PLANNING, 
        notes = null, 
        repeat = 0, 
        media = Media {
            __typename = Media, 
            chapters = null, 
            volumes = null, 
            idMal = 33218, 
            episodes = 1, 
            title = Title {
            __typename = MediaTitle, 
            romaji = Kimi no Koe wo Todoketai
            }
        }
    }
]
*/


public class AniList {
    private static final String BASE_URL = "https://graphql.anilist.co";

    private List<UserMediaQuery.Entry> planningMedia = new ArrayList<UserMediaQuery.Entry>();
    private List<UserMediaQuery.Entry> currentMedia = new ArrayList<UserMediaQuery.Entry>();
    private List<UserMediaQuery.Entry> completedMedia = new ArrayList<UserMediaQuery.Entry>();
    private List<UserMediaQuery.Entry> droppedMedia = new ArrayList<UserMediaQuery.Entry>();
    private List<UserMediaQuery.Entry> pausedMedia = new ArrayList<UserMediaQuery.Entry>();
    private List<UserMediaQuery.Entry> repeatingMedia = new ArrayList<UserMediaQuery.Entry>();

    private OkHttpClient okHttpClient;
    private ApolloClient apolloClient;

    public AniList() {
        okHttpClient = new OkHttpClient.Builder().build();
        apolloClient = ApolloClient.builder()
            .serverUrl(BASE_URL)
            .okHttpClient(okHttpClient)
            .build();
    }

    public void loadUserData(String username, MediaType mediaType) {

        UserMediaQuery mediaQuery = UserMediaQuery.builder()
                                        .username(username)
                                        .type(mediaType)
                                        .build();

        apolloClient.query(mediaQuery).enqueue(new ApolloCall.Callback<Data>() {
                @Override public void onResponse(@NotNull Response<Data> dataResponse) {
                    for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
                        for (UserMediaQuery.Entry entry: data.entries()) {
                            switch(entry.status()) {
                                case PLANNING:
                                    planningMedia.add(entry);
                                    break;
                                case CURRENT:
                                    currentMedia.add(entry);
                                    break;
                                case COMPLETED:
                                    completedMedia.add(entry);
                                    break;
                                case DROPPED:
                                    droppedMedia.add(entry);
                                    break;
                                case PAUSED:
                                    pausedMedia.add(entry);
                                    break;
                                case REPEATING:
                                    repeatingMedia.add(entry);
                                    break;
                            }
                        }
                    }
                }

                @Override
                public void onFailure(@NotNull ApolloException t) {
                    Log.e("AniList Source", t.getMessage(), t);
                }
            }
        );
    }

    public List getPlanningMedia() {
        return planningMedia;
    }


    public List getCurrentMedia() {
        return currentMedia;
    }


    public List getCompletedMedia() {
        return completedMedia;
    }


    public List getDroppedMedia() {
        return droppedMedia;
    }


    public List getPausedMedia() {
        return pausedMedia;
    }

    public List getRepeatingMedia() {
        return repeatingMedia;
    }
}

This prints the data however when you print it in the getter function it does not print the data. 这会打印数据,但是当您在getter函数中打印数据时,它不会打印数据。

            for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
                for (UserMediaQuery.Entry entry: data.entries()) {
                    if (entry.status() == MediaListStatus.PLANNING) {
                        planningMedia.add(entry);
                        Log.d("planning", planningMedia.toString());
                    }
                }
            }

apolloClient.query(mediaQuery).enqueue executes code in async, which means it enqueues the request and so the array list will be empty until the request is being done and is successful from another thread. apolloClient.query(mediaQuery).enqueue以异步方式执行代码,这意味着它使请求入队,因此数组列表将为空,直到请求完成并从另一个线程成功为止。

You might be using the getter directly without waiting for the callback to finish. 您可能直接使用getter而不等待回调完成。 If that's the case check out the following possible solutions. 如果是这种情况,请检查以下可能的解决方案。

If you are waiting for the request to finish to display something on your UI, you might want to bind the data to UI again by using something like notifyDataSetChanged() in case of RecyclerView for instance. 如果您正在等待请求完成以在UI上显示某些内容,则可能希望通过使用诸如notifyDataSetChanged()类的notifyDataSetChanged()例如在RecyclerView的情况下notifyDataSetChanged()将数据再次绑定到UI。 Similarly, if you want to use this data for something else you should probably use a callback mechanism. 同样,如果您想将此数据用于其他用途,则可能应该使用回调机制。

Hope this helps !! 希望这可以帮助 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM