0

I have been searching google and stack overflow for an answer to this question but haven't found any answers. I'm building an android app and need an auto complete field that returns suggestions for city names to be used in a search. The only guides and examples I have found only show how to implement AutocompleteTextView with a String array resource.


I already know how to make the REST call itself and parse the returning data. I just want to know the best way to implement it in an autocompleteview. The url that makes the RESTful call simply takes the city prefix that needs to be more than 1 character and returns the results in JSON.

Example:
URL: www.mysite.com/cities/state_id/mi

Returns:

[{"city_id":6595,"city":"Mi Wuk Village"},{"city_id":6877,"city":"Michigan Bluff"},{"city_id":6724,"city":"Middletown"},{"city_id":6594,"city":"Midpines"},{"city_id":5047,"city":"Midway City"},{"city_id":7415,"city":"Milford"},{"city_id":7337,"city":"Mill Creek"},{"city_id":6295,"city":"Mill Valley"},{"city_id":5857,"city":"Millbrae"},{"city_id":7183,"city":"Mills Orchard"},{"city_id":7339,"city":"Millville"},{"city_id":6376,"city":"Milpitas"},{"city_id":7338,"city":"Mineral"},{"city_id":5698,"city":"Minkler"},{"city_id":4208,"city":"Mint Canyon"},{"city_id":4406,"city":"Mira Loma"},{"city_id":3687,"city":"Miracle Mile"},{"city_id":3945,"city":"Mirada"},{"city_id":5631,"city":"Miramonte"},{"city_id":6802,"city":"Miranda"},{"city_id":4201,"city":"Mission Hills"},{"city_id":6270,"city":"Mission Rafael"},{"city_id":5063,"city":"Mission Viejo"}]


I was thinking about doing something similar to this implementation with SQLLite but this code seems very inefficient since it creates a new adapter everytime the text changes in the CustomAutoCompleteTextChangedListener which implements TextWatcher.

Any input would be much appreciated.

BNK
  • 23,994
  • 8
  • 77
  • 87
Balthasar
  • 3
  • 5
  • https://www.codeofaninja.com/2013/12/android-autocompletetextview-custom-arrayadapter-sqlite.html : the adapter of the example maybe the same, change MyObject to a class whit to fields (city_id and city) use gson library to parse json and get MyObject as City – Rolando Corratge Nieves Oct 07 '15 at 22:10
  • @RolandoCorratgeNieves Yeah that was what I was thinking about doing, I just don't like his overridden implementation of the onTextChanged method. Creating an instance of my custom adapter everytime the user changes his city prefix just sounds expensive. – Balthasar Oct 07 '15 at 22:28
  • implement set_data in the adapter this.data = data; adapter.notifyDataSetChanged(); – Rolando Corratge Nieves Oct 07 '15 at 22:38
  • Apply a handler that runs only certain time i.e 500 milliseconds after user stop writting – Max Pinto Oct 08 '15 at 02:07
  • Read my new answer [here](http://stackoverflow.com/questions/33047156/how-to-create-custom-baseadapter-for-autocompletetextview/33049491#33049491) – BNK Oct 10 '15 at 02:03
  • @BNK This is great thanks a lot, I've been trying to edit the example like Rolando said but I've been having a ton of problems. I'll try to implement your code later and get back to you if it works out for me. – Balthasar Oct 11 '15 at 03:07

1 Answers1

0

The adapter of the example maybe the same, with litle modifications change MyObject to a class whit to fields (city_id and city) use gson library to parse json and get MyObject as City

Creating an instance of my custom adapter everytime the user changes his city prefix just sounds expensive

implement setData in the adapter or a similar method

this.data = data;

and when update and call

adapter.setData()
adapter.notifyDataSetChanged();
Rolando Corratge Nieves
  • 1,233
  • 2
  • 10
  • 25