无法在android

时间:2017-04-11 08:47:35

标签: android autocomplete autocompletetextview

我在我的应用中使用autoCompleteTextView。我想在AutoCompleteTextView中填充城市,我从服务器获取城市。我创建了一个arraylist并在其上设置了城市,然后在autoCompleteTextView上设置了适配器,但无法填充它。我无法理解为什么。

//代码

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

     GetAreas getAreas = new GetAreas();
    getAreas.execute();

//在autoTextCompleteView上设置适配器

    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this,android.R.layout.simple_list_item_1,cityArray);
    editCity.setAdapter(adapter);

    ArrayAdapter<String> adapterArea = new ArrayAdapter<String>
            (this,android.R.layout.simple_list_item_1,areaArray);
    editArea.setAdapter(adapterArea);
}

//获取城市的代码

private class GetAreas extends AsyncTask<String, Void, Void> {

    ProgressDialog progressDialog;
    String ResposeFromGetAreaApi;

    @Override
    protected Void doInBackground(String... params) {
        //Invoke webservice
        WebService wsc = new WebService();
        ResposeFromGetAreaApi = wsc.GetAreas(serviceToken, "GetAreas");
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        Log.i(TAG, "GetAreas" +ResposeFromGetAreaApi);
        try {

            JSONObject jsonObject = new JSONObject(ResposeFromGetAreaApi);
            JSONArray jsonArrayCity = jsonObject.getJSONArray("Table");
            JSONArray jsonArrayArea = jsonObject.getJSONArray("Table1");

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

                modelCity = new ModelCity();

                JSONObject cityObj = jsonArrayCity.getJSONObject(i);
                {
                  String cityId = cityObj.getString("pkCityId");
                    modelCity.setCityId(cityId);
                    String cityName = cityObj.getString("CityName");
                    modelCity.setCityId(cityName);

                }

                modelCityArrayList.add(modelCity);
            }

            for (int j = 0; j < jsonArrayArea.length(); j++) {

                modelCity = new ModelCity();

                JSONObject areaObj = jsonArrayArea.getJSONObject(j);
                {
                    String cityId = areaObj.getString("cityid");
                    modelCity.setAreaCityId(cityId);
                    String areaId = areaObj.getString("AreaId");
                    modelCity.setAreaId(areaId);
                    String areaName = areaObj.getString("AreaName");
                    modelCity.setAreaName(areaName);

                }

                modelAreaArrayList.add(modelCity);
            }

        }
        catch (Exception e)
        {

        }

        progressDialog.dismiss();
    }

3 个答案:

答案 0 :(得分:0)

请阅读Asynctask(用于从主线程卸载任务)。由于您正在执行任务并平行设置适配器,因此城市和区域阵列的数组大小均为0。收到并解析响应后,在onPostExecute中设置适配器。希望这有帮助。

答案 1 :(得分:0)

相当长,但我这样做了。希望你能得到它。

AutoCompleteTextView location;
ArrayAdapter<String> placeslistAdapter;
String PlaceArray[] = {};
ArrayList<String> places;

on onCreate:

location = (AutoCompleteTextView)findViewById(R.id.location);
places = new ArrayList<String>();
placeslistAdapter = new ArrayAdapter<String>(Activity1.this,
            android.R.layout.simple_list_item_1, PlaceArray);
    location.setAdapter(placeslistAdapter);
    location.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {


                try {
                    String searchCode = url
                            + URLEncoder.encode(arg0.toString(), "utf8");
                    AQuery aq = new AQuery(Activity.this);
                    aq.ajax(searchCode, JSONObject.class,
                            getplaceslistcallback());
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });

最后获得回调

private AjaxCallback<JSONObject> getplaceslistcallback() {
    return new AjaxCallback<JSONObject>() {
        @Override
        public void callback(String url, JSONObject object,
                             AjaxStatus status) {
            super.callback(url, object, status);
            Log.d("placesCallBack", object + "");
            if (object != null) {
                try {
                    JSONArray predictions = object
                            .getJSONArray("predictions");
                    places.clear();
                    for (int i = 0; i < predictions.length(); i++) {
                        JSONObject place = predictions.getJSONObject(i);
                        String description = place.getString("description");
                        places.add(description);
                        Log.d("place from google", description);
                    }

                    String[] stringArray = places.toArray(new String[places
                            .size()]);
                    PlaceArray = new String[places.size()];
                    for (int j = 0; j < places.size(); j++) {
                        PlaceArray[j] = places.get(j);
                        Log.d("placearray items",
                                places.get(j) + "  " + PlaceArray[j] + " ,"
                                        + stringArray.toString());
                    }
                    PlaceArray = stringArray.clone();
                    Log.d("placesCallBack", PlaceArray.length
                            + "");
                                        location.setAdapter(placeslistAdapter);
                    placeslistAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
}

答案 2 :(得分:0)

do such like that .. in ur asynctask class..

private class GetAreas extends AsyncTask<String, Void, String> {

    ProgressDialog progressDialog;
    String ResposeFromGetAreaApi;

    @Override
    protected Void doInBackground(String... params) {
        //Invoke webservice
        WebService wsc = new WebService();
        ResposeFromGetAreaApi = wsc.GetAreas(serviceToken, "GetAreas");
try {

            JSONObject jsonObject = new JSONObject(ResposeFromGetAreaApi);
            JSONArray jsonArrayCity = jsonObject.getJSONArray("Table");
            JSONArray jsonArrayArea = jsonObject.getJSONArray("Table1");

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

                modelCity = new ModelCity();

                JSONObject cityObj = jsonArrayCity.getJSONObject(i);
                {
                  String cityId = cityObj.getString("pkCityId");
                    modelCity.setCityId(cityId);
                    String cityName = cityObj.getString("CityName");
                    modelCity.setCityId(cityName);

                }

                modelCityArrayList.add(modelCity);
            }

            for (int j = 0; j < jsonArrayArea.length(); j++) {

                modelCity = new ModelCity();

                JSONObject areaObj = jsonArrayArea.getJSONObject(j);
                {
                    String cityId = areaObj.getString("cityid");
                    modelCity.setAreaCityId(cityId);
                    String areaId = areaObj.getString("AreaId");
                    modelCity.setAreaId(areaId);
                    String areaName = areaObj.getString("AreaName");
                    modelCity.setAreaName(areaName);

                }

                modelAreaArrayList.add(modelCity);
            }

        }
        catch (Exception e)
        {

        }

        return ResposeFromGetAreaApi;
    }

@Override
protected void onPostExecute(String result) {
      Log.i(TAG, "GetAreas" +ResposeFromGetAreaApi);
    if(!result.equals("")){
       ArrayAdapter<String> adapter = new ArrayAdapter<String>
        (this,android.R.layout.simple_list_item_1,cityArray);
  editCity.setAdapter(adapter);
   edtCity.setThreshold(2);

 ArrayAdapter<String> adapterArea = new ArrayAdapter<String>
        (this,android.R.layout.simple_list_item_1,areaArray);
 editArea.setAdapter(adapterArea);
editArea.setThreshold(2);
 }
    progressDialog.dismiss();
  }
 }