微调框列表未显示在android中

时间:2019-09-12 10:02:48

标签: android layout

您好,下面的代码从API的所有建筑物详细信息中获取。从服务器获取响应后,然后将我添加到微调器中。

在下面的代码中要显示建筑物的名称要在微调器中显示。 下面的响应要在微调器列表中显示所有名称。

谁能帮助我做错了事?

来自服务器的响应:

  {
        "list": [
            {
                "ID": "6",
                "Name": "Microsoft1"
            },
            {
                "ID": "9",
                "Name": "building6567"
            }
        ]
    }



private void selectBuilding() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Write code for your refresh logic

                progressDialog = new ProgressDialog (getActivity ());
                progressDialog.setIndeterminate(true);
                progressDialog.setMessage("Communicating...");
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.setCancelable(false);
                progressDialog.show();
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .build();

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(API.URL_BASE)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .client (client)
                        .build();
                API service = retrofit.create (API.class);
                final Call<Managebuilding> userCall = service.getbuildinglist ();
                userCall.enqueue(new Callback<Managebuilding> () {

                    @Override
                    public void onResponse(Call <Managebuilding> call, Response <Managebuilding> response) {

                        if (response != null && response.code ( ) == 200 && response.isSuccessful ( )) {
                            Managebuilding building = response.body ();
                            arrayList = new ArrayList <> ( );
                            arrayList.addAll(building.getList());

                            adapter = new ArrayAdapter<String>(getContext (), android.R.layout.simple_spinner_item, building.getList());///error 

                            spinner.setAdapter(adapter);

                            /** Adding radio buttons for the spinner items */
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                            progressDialog.dismiss ( );

                        } else {
                            progressDialog.dismiss ( );
                            Log.d ("Response errorBody", String.valueOf (response.errorBody ( )));
                        }

                    }


                    @Override
                    public void onFailure(Call<Managebuilding> call, Throwable t) {
                        // lv.setAdapter (adapter);
                        System.out.println("onFailure");
                        System.out.println(t.fillInStackTrace());
                        progressDialog.dismiss();
                        Toast.makeText(getActivity (), "Some error occurred -> ", Toast.LENGTH_LONG).show();;
                        // progressDialog.dismiss();



                    }

                });
            }


        }, 5000);
        return ;
    }

1 个答案:

答案 0 :(得分:0)

创建一个pojo类

public class SpinnerUtils {
     public SpinnerUtils(String spinnerText, int value) {
         this.spinnerText = spinnerText;
         this.value = value;
     }

     public String getSpinnerText() {
         return spinnerText;
     }

     public int getValue() {
         return value;
     }

     public String toString() {
         return spinnerText;
     }

     String spinnerText;
     int value;
 }

然后

SpinnerUtils items[]=new SpinnerUtils[array.length()];
    items[i] = new SpinnerUtils(c.getString("name"), c.getInt("id"));   

    for (int i = 0; i <array.length(); i++) {

items[i] = new SpinnerUtils(c.getString("name"), c.getInt("id"));                       
                }


         ArrayAdapter<SpinnerUtils> adapter = new ArrayAdapter<SpinnerUtils>(this,android.R.layout.simple_spinner_item, items);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                s.setAdapter(adapter);

其中s是微调器,而array是从API检索的JSONArray。要获取ID:

 SpinnerUtils d = items[position];
             int   standard = d.getValue(); 

在微调器的setOnItemSelectedListener中创建上述两行以获取ID,希望它可以工作。