1

Hi I have one issue in my AutoCompleteTextView with AsynTask web service.

If i type the word in AutoCompleteTextView very fast in Textwatcher, each time it goes to web service to fetch the data and displayed in Dropdown.

But if i type very fast,the dropdown shows previous data and again shows the current data in AutocompleteTextview and data is interrupted.How to avoid this issue in Android?

 autoCompleteTextView1.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    itemClicked = false;
                    if (!beforeTextChanged.equals(s.toString()) && !selectedText) { //Here we check selectedText
                        System.out.println("itemClicked" + itemClicked);
                        sText = s.toString();
                        if (sText.length() > 2) {
                            try {
                                autoCompleteTextView1.dismissDropDown();
                                autoCompleteTextView1.setAdapter(null);
                                 Async1 = new NewAsync1(getActivity(),sText);
                                 Async1 .execute();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            try {
                                loadprogress_bar.setVisibility(View.INVISIBLE);
                                arrayList.clear();
                                adapter.clear();
                                adapter = new AutoCompleteAdapter(getActivity(), R.layout.customer_auto, R.id.customerNameLabel, arrayList);
                                autoCompleteTextView1.setAdapter(null);
                                autoCompleteTextView1.dismissDropDown();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    selectedText = false; //Clear selectedText flag
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    itemClicked = false;
                    beforeTextChanged = s.toString();
                }

                @Override
                public void afterTextChanged(Editable s) {
                    itemClicked = false;
                }
            });


      // NewOrderAsync Webservice
        public class NewAsync1 extends AsyncTask<String, String, String> {
            Activity mActivity;
            JSONObject jObject;
            String value;
            ProgressDialog progressDialog;
            public NewAsync1 (Activity mActivity,String value)
            {
                this.mActivity=mActivity;
                this.value= value;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method
                super.onPreExecute();

                loadprogress_bar.setVisibility(View.VISIBLE);
            }
            @Override
            protected String doInBackground(String... params) {
                try {

                    String restUrl = "";
                    System.out.println("searchurl"+Url);
                    response = client.getJSONfromURL(restUrl);
                }  catch (Exception e) {
                    e.printStackTrace();
                }

                return response;
            }

            @Override
            protected void onPostExecute(String response) {
                // TODO Auto-generated method stub
                super.onPostExecute(response);
                loadprogress_bar.setVisibility(View.INVISIBLE);
                try{
                    JSONObject obj=new JSONObject(response);
                    JSONArray arr=obj.getJSONArray("ItemResult");
                    arraylist=ApiCalls.getArraylistfromJson(arr.toString());
                    arrayList = new ArrayList<String>();
                    for (int i = 0; i < arr.length(); i++) {
                        JSONObject obj1=arr.getJSONObject(i);
                        arrayList.add(obj1.getString("Name"));
                }
                //Create adapter

                    adapter = new AutoCompleteAdapter(getActivity(), R.layout.customer_auto, R.id.customerNameLabel, arrayList);
                    autoCompleteTextView1.setThreshold(1);
                    autoCompleteTextView1.setAdapter(adapter);
            }catch (JSONException e){
                e.printStackTrace();
            }

                if (!autoCompleteTextView1.isPopupShowing()) {
                    autoCompleteTextView1.showDropDown();
                }
            }
        }
rajeshlawrance
  • 479
  • 2
  • 6
  • 24

0 Answers0