2017-04-11 4 views
-2

내 응용 프로그램에서 autoCompleteTextView를 사용하고 있습니다. 도시를 AutoCompleteTextView에 채우고 싶습니다. 서버에서 도시를 가져옵니다. arraylist를 만들고 도시를 설정 한 다음 autoCompleteTextView에 어댑터를 설정했지만 채울 수는 없습니다. 나는 이유를 이해할 수 없다.android의 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(); 
    } 

답변

0

작업을 하역에 사용되는 (Asynctask을 읽어 보시기 바랍니다 가져 메인 스레드에서). 당신이 당신의 작업을 실행하고 평행하게 t 그는 어댑터 배열의 크기는 도시와 지역 배열 모두 0입니다. 응답을 받고 구문 분석 한 후 어댑터를 onPostExecute에 설정하십시오. 도움이 되셨을 것입니다.

0

꽤 긴 기간이지만 나는 이렇게 만들었습니다. 당신이 그것을 얻을 수 있기를 바랍니다. 에서 onCreate에서

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

:

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