微调框未显示Android

时间:2018-09-18 07:02:42

标签: android android-studio spinner android-spinner

我正在使用微调器显示来自json的数据,但在屏幕上什么也不显示。我是第一次使用,所以我想分享我为微调器所做的事情。我的代码是:

在XML中:

<Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_location">
    </Spinner>

活动中:

 private Spinner spinner;
 spinner=(Spinner)findViewById(R.id.spinner_location);

  private class GetCity extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... strings) {
        ContentValues param = new ContentValues();
        JSONObject rootObj = parser.makeHttpUrlRequest(Constants.GET_CITY_LIST, "GET", param);
        System.out.println("Result"+rootObj);
        try{
            if(rootObj != null) {
                String status = rootObj.getString(Constants.SVC_STATUS);
                if (Constants.STATUS_SUCCESS.equals(status)) {
                    JSONArray jsonArray = rootObj.getJSONArray("lstCity");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String city=jsonObject.getString("city");
                        cityLst.add(city);

                    }
                }
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return null;
    }
      @Override
    protected void onPostExecute(String s) {

        spinner.setAdapter(new ArrayAdapter<String>
                (DoctorListActivity.this, android.R.layout.simple_spinner_dropdown_item, cityLst));
    }

我在onCreate()中调用此类。 请尽快告诉我最佳答案。谢谢。

3 个答案:

答案 0 :(得分:0)

尝试移动

spinner.setAdapter(new ArrayAdapter<String>
                    (MainActivity.this, android.R.layout.simple_spinner_dropdown_item, cityLst));

更改为onPostExecute,并将doInBackground的返回类型从String修改为ArrayList

要了解AsyncTask的工作原理,请阅读official doc,它是一个很好的例子。

已编辑 在使用cityLst之前,您可以像这样初始化它:

cityLst = new ArrayList<>();

答案 1 :(得分:0)

我希望这对您有用。

在您的Activity中初始化变量

Spinner spinner;
ArrayList<String> cityArraylist;
ArrayAdapter<String> stringArrayAdapter; 

按照“活动”中的以下说明添加AsyncTaskHandler

public class AsyncTaskCity extends AsyncTask<Object, Object, ArrayList<String>> {

        Handler handler;
        ArrayList<String> cityLst;

        public AsyncTaskCity(Handler handler) {
            this.handler = handler;
            cityLst = new ArrayList();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected ArrayList<String> doInBackground(Object... objects) {
             ContentValues param = new ContentValues();
        JSONObject rootObj = parser.makeHttpUrlRequest(Constants.GET_CITY_LIST, "GET", param);
        System.out.println("Result"+rootObj);
        try{
            if(rootObj != null) {
                String status = rootObj.getString(Constants.SVC_STATUS);
                if (Constants.STATUS_SUCCESS.equals(status)) {
                    JSONArray jsonArray = rootObj.getJSONArray("lstCity");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String city=jsonObject.getString("city");
                        cityLst.add(city);

                    }
                }
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }
            return cityLst;
        }


        @Override
        protected void onPostExecute(ArrayList<String> contactses) {
            super.onPostExecute(contactses);
            Message message = new Message();
            message.obj = contactses;
            handler.sendMessage(message);

        }
    }


    public class cityHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.obj==null){
                return;
            }
            ArrayList<String> localConatcts= (ArrayList<String>) msg.obj;
            cityArraylist.addAll(localConatcts);
            stringArrayAdapter.notifyDataSetChanged();
        }
    }

在您的Activity onCreate()方法调用中

        spinner = (Spinner) findViewById(R.id.spinner);
        cityArraylist=new ArrayList<>();
        new AsyncTaskCity(new cityHandler()).execute((Object[]) null);
        stringArrayAdapter=new ArrayAdapter<String>(MySpinnerTest.this, android.R.layout.simple_spinner_dropdown_item, cityArraylist);
        spinner.setAdapter(stringArrayAdapter);

并确保将数据从doInBackground方法的json解析中添加到arraylist中。

答案 2 :(得分:0)

    Try this one.You can get the city in spinner




   ArrayList<String> listCity = new ArrayList<>();
     ArrayAdapter<String> adapterCity;
     Spinner  spn_city;

     spn_city=(Spinner)findViewById(R.id.spinner_location);

     GetCityextends bt = new GetCityextends();
            bt.execute();


  adapterCity = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listCity);
            adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spn_city.setAdapter(adapterCity );


    private class GetCityextends AsyncTask<Void, Void, Void> {
            ArrayList<String> list;

            protected void onPreExecute() {
                super.onPreExecute();
                list = new ArrayList<>();
            }

            protected Void doInBackground(Void... params) {
                InputStream is = null;
                String result = "";
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpPost(Config.Get_CITY);
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    // Get our response as a String.
                    is = entity.getContent();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //convert response to string
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        result += line;
                    }
                    is.close();
                    //result=sb.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // parse json data
                try {
                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject jsonObject = jArray.getJSONObject(i);
                        // add city name to arraylist
                        list.add(jsonObject.getString("city"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(Void result) {
                listCity.addAll(list);
                adapterCity.notifyDataSetChanged();
            }
        }