2017-04-11 1 views
-2

J'utilise autoCompleteTextView dans mon application. Je veux peupler la ville dans AutoCompleteTextView et je reçois la ville du serveur. J'ai créé un arraylist et ai placé des villes là-dessus et ai placé l'adapteur sur autoCompleteTextView mais pas capable de le peupler. Je ne suis pas capable de comprendre pourquoi.Impossible de remplir le texte dans autoCompleteTextView dans android

// Code

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

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

// adaptateur de réglage sur 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); 
} 

Code // pour récupérer les villes

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

Répondre

0

S'il vous plaît lire Asynctask (qui est utilisé pour les tâches de déchargement à partir du fil principal). Depuis que vous exécutez votre tâche et en réglant parallèlement t L'adaptateur de la taille de la matrice est 0 pour la matrice de la ville et de la zone. Faites le réglage de l'adaptateur dans onPostExecute après avoir reçu et analysé la réponse. Espérons que cela aide.

0

Assez long mais je l'ai fait fonctionner de cette façon. J'espère que vous l'aurez.

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

En 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 

     } 
    }); 

enfin obtenir le rappel

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