1

我有一个使用 ArrayAdapter 的 AutocompleteTextView。当文本更改时,适配器会从 Web 服务更新。此更新是在 AsyncTask 中完成的,这应该是一个很好的做法。这或多或少是有效的,因为每次按下按键后的建议都是基于在上一次按下的按键中检索到的字符串。这个页面有几个相关的问题,但没有一个答案对我有用。无论如何,我有一个解决方案,但效率低下,我不知道为什么“官方解决方案”会失败。

我认为关键在于在后台更新 de ArrayAdapter 的功能。这就是我在对 web 服务的异步调用中所做的:

private class DoAutoCompleteSearch extends AsyncTask<String, Void, Map<String, String>> {

    @Override
    protected Map<String, String> doInBackground(String... params) {

        // Ask the webservice for data
        Map<String, String> autoComplete = GetResource.dataList(params[0]);
        return autoComplete;
    }

    @Override
    protected void onPostExecute(Map<String, String> result) {

        //mAutoCompleteAdapter.clear();    * This should work but does not *

/* If it is set a new adapter in the AutoCompleteTextView, the whole thing works properly */

    mAutoCompleteAdapter = new ArrayAdapter<String>(mAutoCompleteAdapter.getContext(), android.R.layout.simple_dropdown_item_1line);
    mACTV.setAdapter(mAutoCompleteAdapter);

        for (Map.Entry<String, String> entry : result.entrySet()) {
            mAutoCompleteAdapter.add(entry.getKey());
        }
    }
}

我已经尝试使用 mAutoCompleteAdapter.clear() 并在任何地方设置 mAutoCompleteAdapter.notifyDataSetChanged() ,但它没用。

4

2 回答 2

2

我也尝试了很多让这种方法发挥作用,但没有比你自己更成功。但我找到了另一种方法来完成你想要的;扩展 ArrayAdapter 并实现 Filterable。当在工作线程中由 AutoCompleteTextView 调用时,此类将执行从数据库中的实际获取:

public class MyAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {

    private List<String> mData = new ArrayList<String>();
    private Server mServer;

    public MyAutoCompleteAdapter(Server server, Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mServer = server;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int index) {
        return mData.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // This method is called in a worker thread

                FilterResults filterResults = new FilterResults();
                if(constraint != null) {
                    try {
                        // Here is the method (synchronous) that fetches the data
                        // from the server
                        List<String> results = mServer.searchForItems(constraint.toString());
                        filterResults.values = results;
                        filterResults.count = results.size();
                    }
                    catch(Exception e) {}

                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if(results != null && results.count > 0) {
                    mData = (List<String>)results.values;
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return myFilter;
    }
}

编辑:我已经改进了上面的代码,因为我有时会遇到异常java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification。为了解决这个问题,我将更新移动mDatapublishResults().

于 2011-08-30T15:48:33.060 回答
0

此代码片段在我的应用程序中对我有用。将其放入包含以下内容的活动中ListView

@Override
public void onActivityResumed() //
{
    if (favoritesHaveChanged(activity, productString)) //
    {
        m_adapter.clear();
        performAsyncTaskWithCallBack(activity);
    }
}

回调将是一个被触发的事件监听器。通常,AsyncTasks您可以onPostExecute()在您的AsyncTask.

于 2011-08-30T19:40:58.937 回答