Search code examples
androidautosuggestautocompletetextview

Android AutoCompleteTextView update


I have an activity which has two inputs : one for city and the other for location. I am storing the city and location information in sqlite table whenever the user request a search operation.

Now, i want whenever the user inputs the city(which is a autosuggest), the autosuggest options in location should automatically update. The input widget for location and city are both AutoCompleteTextView.

How can this be done?

Rgds, Sapan


Solution

  • you just need to provide a CursorAdapter to the AutoCompleteTextView, e.g. a SimpleCursorAdapter (though the SimpleCursorAdapter was buggy when i tried it).

    Cursor cursor = getCityCursor();
    int ids = int[]{R.id.autocomplete_list_item};
    
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
      R.layout.list_item, cursor, new String[]{DB.COL_CITIES});
    
    AutoCompleteTextView actvCities = (AutoCompleteTextView)findViewById(R.id.cities);
    actvCitites.setAdapter(cursorAdapter);
    

    the getCityCursor(); method should provide a cursor obtained by a SQLiteDatabase.query().

    update: the "buggy" SimpleCursorAdapter i mentioned earlier isn't. just provide a CursorToStringConverter. dan breslau wrote a pretty nice article about AutoCompleteTextView and the SimpleCursorAdapter here.