カスタム アダプターを使用して AutoCompleteTextView を実装しました。
getFilter() メソッドでは、サーバーから提案を取得して表示します。ここで、1文字ずつゆっくりと入力すると非常にうまく機能しますが、速く入力し始めると、提案が表示されることがあります。
この問題を処理する方法について何か提案はありますか?
@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;
}