Autocomplete TextView with Asynctask and webservice in android 4

I am trying to do autocomplete using AsyncTaska server. I shared my code here.

This is the code for mine 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 AsyncTaskcode,

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:

  • I can record the results ArrayListfrom AsyncTask(I registered and passed the test). But the values ​​do not appear in the autocomplete drop-down list.
  • I am surprised here, I made an automatic request to the web server to enter more than three characters. After entering more than 3 characters, the request goes, no offers are displayed. But a sentence displaying less than 3 characters entered after the query once occurred.
  • There are no exceptions excluded from my code.

What i tried

  • onPostExecute AsyncTask. .
  • Android 2.3. .

Android 4 AsyncTask doInBackground()?

+5
3

. , , .

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

. , , AutoCompleteTextView ListView . 4.0 (, , ) ListPopupWindow. , .

+8

MyAsyncTask() OnPostExecute()

ArrayList<String> adapterList = new ArrayList<String>();
adapterList=myTask.execute(url).get();
if(!adapterList.isEmpty()){
     .....
}
0
source

All Articles