开发者

AutoCompleteTextView with server response

How to create AutoCompleteTextView which will fetch results from remote server?开发者_开发问答

As I understand I need to implement ArrayAdapter, which must make async requests to server.


AutoCompleteTextView i not meant for this kind of action, i once thought like you and made very bad code with that...

if your auto complete list comes from he network based on every character you input, you better off create a Vertical Linear Layout which contains an EditText and a listView, and create a connection pool.

for every key type check if it was typed in 100-130 ms to make sure the user is likely to wait for a response and issue a request, on any new request, remove or invalidate the last request u've sent.

one response safely arrives , update your list adapter.

works smoothly for me.

AutoCompleteTextview uses a pre made list. it uses AsyncTask itself so i'ts pretty bad to use one yourself to update the list it uses...


1) You have to first make a class for asyncTask and you have to made a connection to remote server in its doInBackground() method

2) you have to make arrayAdapter from response of remoteServer this also you have to make in doInBackground() method

3) after successfully you have to set adapter to AutoCompleteTextView

new AsyncTask<Integer, Void, arrayList> () {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "downloading...", "Please wait...");
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(arrayList result) {
        //make arrayAdapter from result
        //set adapter to AutoCompleteTextView
        progressDialog.dismiss();
        super.onPostExecute(result);
    }

    @Override
    protected arrayList doInBackground(Integer... params) {
        // make connection to remote server
        //retrive response from remote server
        // make arrayList from response

        return arrayList
    }
}.execute(1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜