2

This is my first application in android. I would like to know if this is the best way to do it.I am trying to load a autocomplete textview with the data from mysql db. This data is retrieved from db based on text user enters.

This is the way I am doing it.

1.added a textwatch listener to the autocomplete view. In afterTextchanged method, I am calling an asynchronous task, which connects to db using a php web service and retrieves data from mysql table and loads in a array. On post execute of that task,I am setting the adapter.(Array Adapter).

My question is that, aftertextchanged is invoked for every single letter entered/modified, db call happens here. So, keeping all the database code here, is it good.? It takes time to load the list as well.

Is there any other best way of doing it.

Thanks for your response.

sugra
  • 73
  • 1
  • 6

2 Answers2

2

When a new character is added to the inputbox, cancel the ongoing AsyncTask that fetches the suggestions and start a new one with new parameter. And indicate to the user that the suggestions are being loaded so that user can choose to wait if he wishes to.

Here is a post that explains an ideal way of cancelling a AsyncTask : Ideal way to cancel an executing AsyncTask

Update:

You can achieve a gradual speed up as user types more characters in the editbox if you cache all the results of Asynctask on the first character input(no cancelling required), as that will be the initial list which will contain all the strings starting with that first character. All following results(on new chars i.e. 2nd, 3rd..) will be a subset of this list. Manage the list using an ArrayAdapter. The edit box should be an AutoCompleteTextView.

This way you need to run asynctask only for first character.

Update 2: You can try this code in your onCreate. Sounds like a better option..

    AutoCompleteTextView act = 
                        (AutoCompleteTextView)findViewById(R.id.act_contact);
    ContentResolver content = getContentResolver();
    Cursor cursor = content.query(
                            ContactsContract.Contacts.CONTENT_URI, 
                            PEOPLE_PROJECTION, null, null, null);
    ContactListAdapter adapter = new ContactListAdapter(this, cursor);
    act.setAdapter(adapter);
Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
0

You may try using Android local DB to store data from your server, and then use the local DB to populate your ArrayAdapter, this will definitely improve performance.

Hope it helps ...

sharadendu sinha
  • 827
  • 4
  • 10
  • thanks...i feel as i have to retrieve the record as and when the user types, retrieving from remote db, storing again in local db, will also consume time..please correct me if I am wrong...thanks again!! – sugra Sep 24 '12 at 07:14
  • No you will be only fetching data to be shown in UI from local DB. The local DB may get populated after application launch in background or in some other convenient time. – sharadendu sinha Sep 24 '12 at 08:01
  • Also you will only be retrieving from remote DB once, after that there will be no need for you to go remote for providing Auto complete feature. Will be a great help if Net connectivity is poor! – sharadendu sinha Sep 24 '12 at 08:04
  • Ok..I will try this as well and decide on which is better for my apps...cheers and thanx:-) – sugra Sep 24 '12 at 08:16