如何使用Android中的webservice纠正AutoCompleteTextView中的搜索下拉列表?

时间:2016-01-05 13:56:38

标签: android android-asynctask autocompletetextview android-textwatcher

您好我的AutoCompleteTextView中有一个问题与AsynTask Web服务。

如果我在Textwatcher中以非常快的速度在AutoCompleteTextView中输入单词,每次进入Web服务以获取数据并显示在Dropdown中。

但如果输入速度非常快,下拉列表会显示以前的数据并再次显示AutocompleteTextview中的当前数据并且数据会中断。如何在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();
                }
            }
        }

0 个答案:

没有答案