In the program I use autocomplete. Below is a lot of code, but I hope someone has encountered a similar problem, or will master it ;-)

To save the number of requests, I perform a request to the server only on the second letter and each multiple of the 4th, as well as if the last entered character is a space or a hyphen.

The problem is that at the right moments not one request is sent, but two.

Inserted two PlaceProvider.java logs (at the very bottom):

Log.e("AUTOCOMPLETE", String.valueOf(resultList)); 

and GooglePlacesAutocompleteAdapter -> getFilter () -> performFiltering (CharSequence constraint)

 Log.e("AUTOCOMPLETE", "___Last: " + lastSym + " " + String.valueOf(constraint) + constraint.length()); 

I enter 4 letters:

enter image description here

Result in the log

 05-24 20:44:16.077 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: ___Last: н Магн4 05-24 20:44:16.077 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: [Малое Козино, Нижегородская область, Россия, Магнитогорск, Челябинская область, Россия, Малаховка, Московская область, Россия, Малоярославец, Калужская область, Россия, Мантурово, Костромская область, Россия] 05-24 20:44:16.845 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: ___Last: н Магн4 05-24 20:44:16.845 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: [Магнитогорск, Челябинская область, Россия, Магнитка, Челябинская область, Россия, Магнитный, Курская область, Россия, Магнитский, Челябинская область, Россия, Магна, Республика Калмыкия, Россия] 

Judging by the log it turns out that the request is sent twice, and the result of the execution of requests is different. With what it can be connected?

Application code:

Activate:

  final GooglePlacesAutocompleteAdapter googlePlacesAutocompleteAdapter = new GooglePlacesAutocompleteAdapter(this, R.layout.autocomplete_city_textview_item); chooseCity = (DelayedAutoCompleteTextView) view.findViewById(R.id.editTextCityName); chooseCity.setAdapter(googlePlacesAutocompleteAdapter); 

PlacePprovider.java

 public class PlaceProvider { private final static String TAG = PlaceProvider.class.getSimpleName(); private final static String PLACE_API_BASE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json"; private final static String API_KEY = MyApplication.getAppContext().getString(R.string.place_provider_key); ArrayList<String> resultList = null; public ArrayList<String> autocomplete(String input) { try { String URL = PLACE_API_BASE_URL + "?key=" + API_KEY + "&input=" + URLEncoder.encode(input, "utf8") + "&types=(cities)"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(URL) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Log.d(TAG, "Request to Google Place API failure"); } @Override public void onResponse(Response response) throws IOException { String jsonData = response.body().string(); Log.v(TAG, jsonData); try { JSONObject jsonObject = new JSONObject(jsonData); JSONArray jsonArray = jsonObject.getJSONArray("predictions"); resultList = new ArrayList<String>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { Log.i(TAG, jsonArray.getJSONObject(i).getString("description")); resultList.add(jsonArray.getJSONObject(i).getString("description")); } } catch (JSONException e) { Log.e(TAG, e.getLocalizedMessage()); } } }); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.getLocalizedMessage()); } Log.e("AUTOCOMPLETE", String.valueOf(resultList)); return resultList; } } 

Adapter

 public class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable { private List<String> resultList; private PlaceProvider mPlaceProvider; private final static int MAX_RESULT = 10; private Context mContext; public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); mContext = context; resultList = new ArrayList<String>(); mPlaceProvider = new PlaceProvider(); } @Override public int getCount() { return resultList.size(); } @Override public String getItem(int position) { return resultList.get(position); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(mContext); convertView = inflater.inflate(R.layout.autocomplete_city_textview_item, parent, false); } TextView description = (TextView) convertView.findViewById(R.id.autocomplete_list_item); description.setText(getItem(position)); return convertView; } @Override public Filter getFilter() { return new Filter() { FilterResults filterResults = new FilterResults(); @Override protected FilterResults performFiltering(CharSequence constraint) { if (constraint != null) { char lastSym = constraint.charAt(constraint.length() - 1); if ((constraint.length() % 4 == 0) || (lastSym == ' ') || (lastSym == '-') || (constraint.length() == 2)) { List<String> predictions = mPlaceProvider.autocomplete(constraint.toString()); filterResults.values = predictions; filterResults.count = predictions.size(); } } return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results != null && results.count > 0) { resultList = (List<String>) results.values; notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } }; } } 

    1 answer 1

    Understood: the code was a different developer - these are the flaws of the old code