簡體   English   中英

Android:AutoCompleteTextView和過濾器 - 鍵入延遲 - 為什么?

[英]Android: AutoCompleteTextView and Filter - Typing Delay - Why?

好吧,伙計們,這讓我發瘋,我無法在互聯網上找到解決方案!
我做了一個新的Eclipse項目,並在里面粘貼了這個代碼:

https://developers.google.com/places/training/autocomplete-android

它可以工作,但是如果我輸入或刪除(a)字符,那么在鍵入它們時大多會有延遲(短暫凍結)並且建議 - 下拉消失並且大部分再次出現。
至於我的理解,過濾本身是異步完成的,因此來自后台線程,為什么短暫的凍結呢?

我的目標是在Google Play商店中擁有一個無凍結的AutoCompleteTextView。
所以你們有任何建議/解決方法來實現這一目標嗎?

您必須使用以下庫文件。您必須在AutoCompletetextView上調用addTextChangeListener(Listener) afterTextChanged(Editable s)里面你必須提到你需要做的功能。

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.text;

/**
 * When an object of a type is attached to an Editable, its methods will
 * be called when the text is changed.
 */
public interface TextWatcher extends NoCopySpan {
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after);
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void onTextChanged(CharSequence s, int start, int before, int count);

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
     * to mark your place and then look up from here where the span
     * ended up.
     */
    public void afterTextChanged(Editable s);
}

我通過放置這些代碼行來解決它(請參閱注釋):

            @Override
            protected FilterResults performFiltering(CharSequence constraint)
            {
                FilterResults filterResults = new FilterResults();

                // ADDED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                ArrayList <String> resultListTemp = new ArrayList <String> ();

                if (constraint != null)
                {
                    // ADDED CODE: TO STOP TYPING DELAY & TO FIX ERROR: "The content of adapter has changed but ListView did not receive a notification"
                    if (conn != null)
                    {
                        conn.disconnect();
                    }

                    // CHANGED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                    resultListTemp.addAll(autocomplete(constraint.toString()));

                    filterResults.values = resultListTemp;
                    filterResults.count = resultListTemp.size();
                }
                return filterResults;
            }


            @Override
            protected void publishResults(CharSequence constraint, FilterResults results)
            {
                // ADDED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                resultList = (ArrayList <String>) results.values;

                if (results != null && results.count > 0)
                {
                    notifyDataSetChanged();
                }
                else
                {
                    notifyDataSetInvalidated();
                }
            }


            private ArrayList<String> autocomplete(String input)
            {
                // CHANGED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                ArrayList<String> resultList = new ArrayList <String> ();
                .....
                .....
            }



其中“conn”是用於獲取Google商家信息數據的HttpURLConnection。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM