Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete TextView with Asynctask and webservice in android 4

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()?

like image 243
Akilan Avatar asked Jul 18 '12 07:07

Akilan


1 Answers

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

like image 121
Infernus Avatar answered Sep 28 '22 07:09

Infernus