Search code examples
androidasynchronousretrofit2autocompletetextview

AutoCompleteTextView : Load list first then query on client or update it through Async Calls?


I have an activity that contains an AutoCompleteTextView.

This AutoCompleteTextView is a list of thousands and thousands of entries.

There are 2 possible solutions:

1- Load the huge list first and populate the AutoCompleteTextView.

2- Async Calls when the user is writing and updating the AutoCompleteTextView adapter dynamically.

Other than the obvious cons of 1 and 2 (Huge load on the client in 1 and huge load on the server in 2). Which is the most optimal solution and the one most considered when handling such a case?


Solution

  • 1- Load the huge list first and populate the AutoCompleteTextView.

    Best if you load the data "outside" the app with a SyncAdapter and update your local SQLite database, so that the AutoCompleteTextView can use a CursorAdapter to get the data. This is a good option if your list doesn't change all that much. However, it mandates all the complexity of maintaining a local database and using SyncAdapter including unattended authentication, possibly with a custom authenticator.

    2- Async Calls when the user is writing and updating the AutoCompleteTextView adapter dynamically.

    This is what we do in our app; I would recommend this approach. Here are some pointers:

    • Limit the number of results you send back on any single request, say around twenty. In our app, some common items are requested more often than others, so we prioritize the results to include those items. Your user isn't going to scroll very far through the list anyway; if their desired item isn't included, they can type another character or two to get it.

    • Put a timer on the AutoCompleteTextView so that the user has to stop typing for 200-250 milliseconds before the request is sent to the server. This will help to ensure that the request is sent only when it's necessary and cut down on server bandwidth.