微调器中仅显示阵列的列表项

时间:2019-02-24 17:35:59

标签: android arrays api arraylist

我正在开发中,要求列出其所在城市的所有柜台,为此,我使用了 battuta Api,我设法拿到了微调器,但不幸的是,只有最后一个项目出现在此处(微调)

for (int i=0; i<response.length(); i++){
                        try {
                            worldlist = new ArrayList<String>();
                            ID = response.getJSONObject(i).getString("code");
                            name = response.getJSONObject(i).getString("name");
                            // Retrieve the name on the Spinne
                            worldlist.add(name);
                            System.out.println("Name\n"+name);
                          //  System.out.println("WorldList"+worldlist.toString());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                       ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, worldlist);
                       spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
                       spinner.setAdapter(spinnerArrayAdapter);
                       spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                           @Override
                           public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


                           }

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

                           }
                       });



                   }




                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            System.out.println("Error"+error.getMessage());


        }
    });

    mQueue.add(request);

1 个答案:

答案 0 :(得分:1)

我认为您的逻辑代码不正确,请尝试将wordlist声明和int旋转器适配器放入for循环之外。

worldlist = new ArrayList<String>();

for (int i = 0; i < response.length(); i++) {
    try {
        ID = response.getJSONObject(i).getString("code");
        name = response.getJSONObject(i).getString("name");

        // Retrieve the name on the Spinne
        worldlist.add(name);

        System.out.println("Name\n"+name);
        // System.out.println("WorldList"+worldlist.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, worldlist);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


    }

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

    }
});