好吧,夥計們,這讓我瘋狂,我無法在互聯網上找到解決方案!
我做了一個新的Eclipse項目,並粘貼在代碼裏:
https://developers.google.com/places/training/autocomplete-android
它的工作原理,但如果我進入或打字時刪除其中(一)字符(縣)有主要的延遲(短凍結)和建議 - 下拉菜單消失並且大部分再次出現。
至於我的理解,過濾本身是異步完成的,因此從後臺線程,爲什麼短暫凍結?
我的目標是在Google Play商店中擁有一個無凍結的AutoCompleteTextView。
那麼你們有什麼建議/解決方法來實現這一目標嗎?Android:AutoCompleteTextView和過濾器 - 鍵入延遲 - 爲什麼?
回答
我解決它通過將這些代碼(見註釋):
@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類。
您必須使用以下庫文件。您必須在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);
}
如果它解決了你的問題。對我的回答保持投票,鼓勵所有人回答你。 –
謝謝raguM.tech。 我已經嘗試過了:我將Google Places請求包裝在一個AsyncTask中,並在afterTextChanged中調用它,這可行,但對於我輸入的每個字符,下拉列表消失並再次出現(我怎樣才能避免這種情況,列表保持打開狀態?),稍後我已經閱讀,AutoCompleteTextView已經自動連接到TextWatcher,並且不需要添加它並使用Filter來代替。現在我有點失去了使用什麼方法。有什麼想法嗎? – DroidFox
那麼你必須使用EndlessAdapter。 http://developer.android.com/reference/android/widget/Filter.html http://developer.android.com/reference/android/widget/CursorAdapter.html –
- 1. 數據表延遲加載服務器過濾器延遲
- 2. GTK3和Python:爲什麼set_property延遲?
- 3. 爲什麼不延遲工作過程入隊工作?
- 4. SCSS編譯爲什麼會有延遲?
- 5. 爲什麼setTimeout無延遲執行timeExpired?
- 6. 爲什麼asyncio.sleep(延遲)完成超過給定的延遲更早?
- 7. 使用Processing Minim Library添加延遲和ADSR過濾器
- 8. 爲什麼google chromes控制檯延遲?
- 9. 爲什麼RecyclerView的onDraw延遲
- 10. 延遲加載過濾項目
- 11. 觸摸事件爲什麼延遲?
- 12. 延遲鍵盤輸入幫助
- 13. 爲什麼會放大增加延遲?
- 14. 爲什麼粘貼事件值延遲?
- 15. 爲什麼我的延遲工作?
- 16. 爲什麼phantom.exit()有2秒延遲?
- 17. 爲什麼CSS動畫延遲?
- 18. 爲什麼要做:懸停延遲?
- 19. 是什麼延遲加載和load()
- 20. JS按鍵延遲
- 21. 延遲使用QsortFIlterProxymodel過濾QTreeview
- 22. 如何延遲按鍵輸入
- 23. 什麼是P99延遲?
- 24. 什麼是延遲加載?
- 25. 鍵盤按鍵重複率和延遲
- 26. Redis延遲鍵添加
- 27. MeteorJS,鐵路由器,過濾器,重定向,延遲
- 28. arduino伺服器和延遲
- 29. 爲什麼DFA等同於延遲輸入DFA
- 30. 將過濾器應用於MPMoviePlayer無延遲縮略圖
好的......不錯的嘗試.. –
Thanx!此外,在performFiltering中添加臨時「resultListTemp」還將處理錯誤消息:「適配器的內容已更改,但ListView未收到通知」。我認爲Google Places Autocomplete Training應該更新這些內容。 – DroidFox
你提到了adapter.NotifydataSetChange()。如果不使用此函數在ListView中進行更改。那麼只有listview不會更新。 –