我已经使用自定义适配器实现了AutoCompleteTextView。
在getFilter()方法中,我从服务器获取建议并显示它。 现在,如果我慢慢地逐个输入一个字符,它的效果非常好,但是当我开始快速输入时,它会显示出建议。
有关如何处理此问题的任何建议吗?
@Override
public Filter getFilter() {
Log.i("TIME", "Called getfilter");
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i("TIME", "1");
Log.i("TIME", "2 : " + constraint);
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
long t1 = System.currentTimeMillis();
autoCompletItems = autocomplete(constraint.toString());
long t2 = System.currentTimeMillis();
Log.i("TIME", "Required Milis : " + (t2 - t1));
Log.i("TIME", "Total Items : " + autoCompletItems.size());
// Assign the data to the FilterResults
filterResults.values = autoCompletItems;
filterResults.count = autoCompletItems.size();
}
Log.i("TIME", "3: " + filterResults.count);
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
Log.i("TIME", "Called publishResults" + " , Count : "
+ results.count);
clear();
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
public CharSequence convertResultToString(Object resultValue) {
AutoCompletItem autoCompletItem = (AutoCompletItem) resultValue;
return autoCompletItem.getProductName();
}
};
return filter;
}
protected ArrayList<AutoCompletItem> autocomplete(String string) {
ArrayList<AutoCompletItem> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
Log.i(LOG_TAG, "2.1");
StringBuilder sb = new StringBuilder(URL_GET_SUGGESTIONS_SERVICE);
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
InputStream inputStream = conn.getInputStream();
Log.i(LOG_TAG, "2.2");
int available = inputStream.available();
Log.i(LOG_TAG, "2.3" + " Total available : " + available);
byte[] data = new byte[available];
inputStream.read(data);
Log.i(LOG_TAG, "2.4");
ItemContainer container = (ItemContainer) deserialize(data);
Log.i(LOG_TAG, "2.5");
resultList = (ArrayList<AutoCompletItem>) container
.getSerializable();
Log.i(LOG_TAG, "2.6");
System.out.println("Check Data");
} catch (MalformedURLException e) {
Log.i(LOG_TAG, "2.exception " + e);
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.i(LOG_TAG, "2.exception " + e);
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
Log.i(LOG_TAG, "2.finally ");
if (conn != null) {
conn.disconnect();
}
}
return resultList;
}
答案 0 :(得分:-1)
对于那些可能正在寻找答案的人来说,答案真的很晚。
AutoCompleteTextView myAutoCompleteTV;
如果您正在动态填充AutoCompleteTextView(可能使用AsyncTask:以后能够在UI线程上返回),您可以添加如下的TextWatcher
myAutoCompleteTV.addTextChangedListener(this);
这样你就可以在用户输入他知道自己正在寻找的角色时单独填充它。当他遇到困难或需要建议时,他会停下来,当myAutoCompleteTV
不再查询时(请记住我假设你有一个巨大的名单)
在三位观察者之一中,强制过滤器显示符合当前myAutoCompleteTV
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
ArrayAdapter<String> myAdapter = (ArrayAdapter<String>) myAutoCompleteTV.getAdapter
myAdapter.getFilter().filter(myAutoCompleteTV.getText().toString());
myAdapter.notifyDataSetChanged();
}