【问题标题】:Not able to populate text in autoCompleteTextView in android无法在android的autoCompleteTextView中填充文本
【发布时间】:2017-09-06 13:09:10
【问题描述】:

我在我的应用程序中使用 autoCompleteTextView。我想在 AutoCompleteTextView 中填充城市,并且从服务器获取城市。我创建了一个数组列表并在其上设置了城市,然后在 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();
    }

【问题讨论】:

    标签: android autocomplete autocompletetextview


    【解决方案1】:
    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();
      }
     }
    

    【讨论】:

      【解决方案2】:

      相当长,但我让它以这种方式工作。希望你能得到它。

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

      在 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();
                      }
                  }
              }
          };
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-10
          • 1970-01-01
          • 2015-03-10
          • 1970-01-01
          相关资源
          最近更新 更多