0

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

1 回答 1

-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();
}
于 2014-03-11T14:08:03.323 回答