Java 将外部类中的值设置为内部类中检索到的值

Java 将外部类中的值设置为内部类中检索到的值,java,android,global-variables,retrofit2,inner-classes,Java,Android,Global Variables,Retrofit2,Inner Classes,我的android应用程序中有一个片段,其想法是从json文件(github)中提取信息,并将其解析为标题,然后更新到我的recyclerview中。我可以很好地提取数据,将其保存在列表中,这是我的模式 现在,该数据位于“onResponse”内部类中,也就是“onCreateView”内部类中 更新我的recyclerview的代码位于onCreateView内部类中。如何将列表从onResponse传递到onCreate,甚至传递到globallevel 在该类中,我有两个全局变量: sta

我的android应用程序中有一个片段,其想法是从json文件(github)中提取信息,并将其解析为标题,然后更新到我的recyclerview中。我可以很好地提取数据,将其保存在列表中,这是我的模式

现在,该数据位于“onResponse”内部类中,也就是“onCreateView”内部类中

更新我的recyclerview的代码位于onCreateView内部类中。如何将列表从onResponse传递到onCreate,甚至传递到globallevel

在该类中,我有两个全局变量:

static List<GithubRepo> list  = new ArrayList<>();
static List<String> List_body = new ArrayList<>();
static List=new ArrayList();
静态列表列表_body=new ArrayList();
现在,在创建视图的内部类“Create”方法中,我正在使用改型来解析github的json,以获得一些存储库名称

我可以很好地获取它们,但当我从“response.body”中获取列表时,请使用以下命令将其正确解析为仅获取标题的字符串:

private void setList(List<GithubRepo> repo){
    if (repo != null) {
        int counter = 0;
        for (GithubRepo r : repo) {
            String name = r.getName().toString();
            List_body.add(name);
            counter++;
        }


    }
    else{
        Log.d("test:empty", "empty");
    }
}
private void setList(列表回购){
如果(回购!=null){
int计数器=0;
对于(GITUBREPO r:repo){
字符串名称=r.getName().toString();
列出正文。添加(名称);
计数器++;
}
}
否则{
Log.d(“测试:空”、“空”);
}
}
上面的GithubRepo仅仅是json的对象结构,我在内部类中获取名称并设置它们,但是当我尝试将新列表应用到我的视图时,它们仍然为空。如何从内部类中的变量设置全局/静态变量的值

事情是这样的:

public class primary_fragment extends Fragment implements Agg_Adapter.ItemClickListener {

static List<GithubRepo> list  = new ArrayList<>();   <--------HOLDS value of schema object temporarily
static List<String> List_body = new ArrayList<>();   <--------UPDATES the recyclerview, currently empty
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {


.... Some code

        call.enqueue(new Callback<List<GithubRepo>>() {
        @Override    <------------------------- From here is the inner class
        public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
            // 1. start
            if (response.isSuccessful()) {
                if (response.body() != null) {

                    list.addAll(response.body());
                    setList(list);
   });

  Log.d("testedB2!:", (List_body.toString()));  <------Should have the values set but it is null
公共类主\u片段扩展片段实现Agg\u适配器。ItemClickListener{

静态列表=新的ArrayList()您使用改型的方式使调用异步。这没有错,事实上它应该是这样的。但是,假设
List\u body
应该在打印到日志的行中填充,这是不正确的。简单地说,在网络调用完成之前,
log.d
将运行,并且不打印任何内容。

有不同的方法可以解决此问题。最简单的方法是从
onResponse
中调用一个方法,让片段知道列表已准备就绪。例如:

call.enqueue(new Callback<List<GithubRepo>>() {
    @Override    
    public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
        if (response.isSuccessful()) {
            if (response.body() != null) {
                list.addAll(response.body());
                setList(list);
                onListReady();
     });
这可以在片段中实现

就像我说的,有不同的方法来实现这一点。我只是想告诉你调用实际上是异步运行的

private void onListReady () {
     Log.d("testedB2!:", (List_body.toString()));
}