Autocomplete TextView with Asynctask and webservice in android 4
Asked Answered
D

3

5

I am trying to make a autocomplete using AsyncTask with a server. I shared my code here.

This is the code for my AutocompleteTextwatcher:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.mainautocomplete);
textView.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
        String text=charSequence.toString();
        if (text.length() > 3) {
            MyAsyncTask myTask = new MyAsyncTask();
            try {
                ArrayList<String> adapterList = new ArrayList<String>();
                adapterList=myTask.execute(url).get();/*My Web service url*/
                if(!adapterList.isEmpty()){
                    Log.v("ADPTER LIST",adapterList.toString());
                    adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, adapterList);
                    textView.setAdapter(adapter);
                }else{
                    Log.v("ADPTER LIST","No VALUES");
                }
            } catch (InterruptedException e) {
                Log.v("catch",e.getMessage());
            } catch (ExecutionException e) {
                Log.v("catch2",e.getMessage());
            }
        }
    }
});

And my AsyncTask code is,

private class MyAsyncTask extends AsyncTask<String, Void, ArrayList<String>>{
    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(HomeLayoutActivity.this,"", "Please Wait!");
    }

    @Override
    protected ArrayList<String> doInBackground(String... params) {
        try {
            String myQuery=params[0];
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = null;
            InputStream is = null;
            String result = null;
            StringBuilder sb = null;

            response = httpClient.execute(new HttpGet(myQuery));
            is = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
            JSONArray jArray = new JSONArray(result);
            JSONObject jData = null;
            tags.clear();

            for (int i = 0; i < jArray.length(); i++) {
                jData = jArray.getJSONObject(i);
                tags.add(jData.getString("tagname"));
            }
            Log.v("JSON",""+tags.toString());

        } catch(Exception e){
            Log.v("MUGBUG4",e.getMessage());
        }
        return tags;
    }

    @Override
    protected void onProgressUpdate(Void...strings ){

    }

    @Override
    protected void onPostExecute(ArrayList<String> result){
        Log.v("OPE",result.toString());
        pd.dismiss();
    }

}

My problem is,

  • I can able to log results of ArrayList from AsyncTask (I logged and checked). But the values are not showing in the autocomplete dropdown.
  • I am surprised here, i made the auto complete request to web server for greater than three characters typed. Once typed more than 3 characters, the request is going, no suggestion is showed. But the suggestion showing for less than 3 characters typed after the request once happened.
  • There is no exceptions thrown from my code.

What i tried is,

  • I tried to set adapter in the onPostExecute of AsyncTask. The same output is showing.
  • I tried with android 2.3 emulator. It working fine.

Is any problem with android 4 and Asynctask doInBackground()?

Dower answered 18/7, 2012 at 7:6 Comment(0)
E
8

I have the same situation. I don't know will it help, but I recommend you to add these lines of code after adding your adapter.

    if (!textView.isPopupShowing()) {
        textView.showDropDown();
    }

It helped me. To be true I don't know exact reason of this behavior, but in earlier implementations of AutoCompleteTextView they used ListView to display dropdown. In 4.0 (and maybe earlier) they start using ListPopupWindow. May be this is the reason. Cheers

Endure answered 4/10, 2012 at 15:44 Comment(2)
I had the same problem, it does show up after setting this before the adapter, but what about notifyDataSetChanged(). Could that work?Conde
Can't figure out how to post code here, but in words - everything should be ok with notifyDataSetChanged(). What problem do you have exactly?Endure
F
0

You need to implement a filter, similar to the answer here:

Android - AutoCompleteTextView only works when backspacing

Finedrawn answered 25/7, 2013 at 17:19 Comment(0)
F
0

Write the following code in MyAsyncTask() OnPostExecute() method

ArrayList<String> adapterList = new ArrayList<String>();
adapterList=myTask.execute(url).get();
if(!adapterList.isEmpty()){
     .....
}
Franchot answered 28/7, 2014 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.