java - 为什么代码没有在用 API 调用的 spinner 中显示列表?

API 传来了一个字符串数组。代码有什么问题?我正在尝试将 value 传递给 spinner

Call<List<ResponseSubject>> 
  subjectOption=ApiClient.getInstance().getApi().subjectOption();
        subjectOption.enqueue(new Callback<List<ResponseSubject>>() {
            @Override
            public void onResponse(Call<List<ResponseSubject>> call, Response<List<ResponseSubject>> response) {

                List<ResponseSubject> responseSubject=  response.body();
            if(responseSubject!=null && responseSubject.size()>0) {

                String[] sub = new String[responseSubject.size()];

                for (int i = 0; i < responseSubject.size(); i++) {
                    sub[i]= Arrays.toString(responseSubject.get(i).getData());

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,sub);
                    spin_class.setAdapter(adapter);

                    spin_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });


                }
            }


        }

        @Override
        public void onFailure(Call<List<ResponseSubject>> call, Throwable t) {

        }
    });

在上面的代码中,它检查来自 API 的 value 是否为空。在检查数据是否传入数组并将该数组数据分配给 spinner 之后。

回答1

我认为这段代码应该在“for”循环之外:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,sub);
spin_class.setAdapter(adapter);

spin_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        
    }
});

最终代码应如下所示:

Call<List<ResponseSubject>> subjectOption = ApiClient.getInstance().getApi().subjectOption();
    subjectOption.enqueue(new Callback<List<ResponseSubject>>() {
        @Override
        public void onResponse(Call<List<ResponseSubject>> call, Response<List<ResponseSubject>> response) {

            List<ResponseSubject> responseSubject=  response.body();
        if(responseSubject!=null && responseSubject.size()>0) {

            String[] sub = new String[responseSubject.size()];

            for (int i = 0; i < responseSubject.size(); i++) {
                sub[i]= Arrays.toString(responseSubject.get(i).getData());


            }
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,sub);
        spin_class.setAdapter(adapter);

        spin_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    @Override
    public void onFailure(Call<List<ResponseSubject>> call, Throwable t) {

    }
});

相似文章

javascript - 为什么这个 const 应该是未定义的?

我有一个计算车辆保险的表格,我已经减少了代码以只显示我遇到的问题(所以如果你看到它不完整,那是因为我减少了它)。我的问题是我有两个输入类型的收音机,这供用户选择两个选项之一,当我尝试将该常量的valu...