3

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

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

它的工作原理,但如果我進入或打字時刪除其中(一)字符(縣)有主要的延遲(短凍結)和建議 - 下拉菜單消失並且大部分再次出現。
至於我的理解,過濾本身是異步完成的,因此從後臺線程,爲什麼短暫凍結?

我的目標是在Google Play商店中擁有一個無凍結的AutoCompleteTextView。
那麼你們有什麼建議/解決方法來實現這一目標嗎?Android:AutoCompleteTextView和過濾器 - 鍵入延遲 - 爲什麼?

回答

1

我解決它通過將這些代碼(見註釋):

  @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將」 是取的谷歌的地方的數據HttpURLConnection類。

+0

好的......不錯的嘗試.. –

+0

Thanx!此外,在performFiltering中添加臨時「resultListTemp」還將處理錯誤消息:「適配器的內容已更改,但ListView未收到通知」。我認爲Google Places Autocomplete Training應該更新這些內容。 – DroidFox

+0

你提到了adapter.NotifydataSetChange()。如果不使用此函數在ListView中進行更改。那麼只有listview不會更新。 –

1

您必須使用以下庫文件。您必須在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); 
} 
+0

如果它解決了你的問題。對我的回答保持投票,鼓勵所有人回答你。 –

+0

謝謝raguM.tech。 我已經嘗試過了:我將Google Places請求包裝在一個AsyncTask中,並在afterTextChanged中調用它,這可行,但對於我輸入的每個字符,下拉列表消失並再次出現(我怎樣才能避免這種情況,列表保持打開狀態?),稍後我已經閱讀,AutoCompleteTextView已經自動連接到TextWatcher,並且不需要添加它並使用Filter來代替。現在我有點失去了使用什麼方法。有什麼想法嗎? – DroidFox

+0

那麼你必須使用EndlessAdapter。 http://developer.android.com/reference/android/widget/Filter.html http://developer.android.com/reference/android/widget/CursorAdapter.html –