自动完成延迟

时间:2012-09-20 07:28:37

标签: android autocomplete

我的应用程序中有自动填充功能。如果我开始输入字母,自动完成没有时间需要大量时间来处理JSON文件。我使用addTextChangedListener,因为这个输入字母不是那么顺利。我想在JSON中的搜索数据之前设置超时。

Object mObj[] = { dep, arr };
        for (int mI = 0; mI < mObj.length; mI++) {
            ((AutoCompleteTextView) mObj[mI])
                    .addTextChangedListener(new TextWatcher() {

                        public void onTextChanged(final CharSequence s,
                                int start, int before, int count) {
                            setListViewAuto(s, mass);
                        }

                        public void beforeTextChanged(CharSequence s,
                                int start, int count, int after) {
                            // TODO Auto-generated method stub
                        }

                        public void afterTextChanged(Editable s) {
                            // TODO Auto-generated method stub
                        }
                    });
        }

UPD: 我这样做:

final Object mObj[] = { dep, arr };
        for (int mI = 0; mI < mObj.length; mI++) {
            ((AutoCompleteTextView) mObj[mI])
                    .addTextChangedListener(new TextWatcher() {

                        private Handler autoHandler = new Handler() {
                            @Override
                            public void handleMessage(Message msg) {
                                if (msg.what == MESSAGE_TEXT_CHANGED) {
                                    String enteredText = (String) msg.obj;
                                    setListViewAuto(enteredText);
                                }
                            }
                        };

                        public  void onTextChanged(final CharSequence s,
                                int start, int before, int count) {
                            massListAuto = new ArrayList<String>(Arrays
                                    .asList(mass));
                            String enteredText = s.toString();
                            autoHandler.removeMessages(MESSAGE_TEXT_CHANGED);
                            final Message msg = Message.obtain(autoHandler,
                                    MESSAGE_TEXT_CHANGED, enteredText);
                            autoHandler.sendMessageDelayed(msg,
                                    AUTOCOMPLETE_DELAY);
                        }

                        public void beforeTextChanged(CharSequence s,
                                int start, int count, int after) {
                            // TODO Auto-generated method stub
                        }

                        public void afterTextChanged(Editable s) {
                            // TODO Auto-generated method stub
                        }
                    });
        }

但是我收到消息:这个Handler类应该是静态的,否则可能会发生泄漏

0 个答案:

没有答案