我已经使用自定义适配器实现了 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;
}