7

我正在使用自定义 ArrayAdapter 在 AutocompleteTextView 上设置适配器(AddressAdapter 扩展了 ArrayAdapter)。

public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> mData;
ArrayList<String> listTempPrefix = new ArrayList<String>();
ArrayList<String> listTemp = new ArrayList<String>();
String valueText;
String[] words;
String ulcase;

public AutoCompleteAdapter(Context context, int textViewResourceId, ArrayList<String> bS) {
    super(context, textViewResourceId);
    mData = bS;//new ArrayList<String>();
}

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

@Override
public String getItem(int index)  
{
    synchronized (listTempPrefix)
    {
        try {
            //Log.e("Error", listTempPrefix.get(index));
            return listTempPrefix.get(index);
        } catch(IndexOutOfBoundsException e) {
            Log.e("Error", "IndexOutOfBoundsException");
            return "";
        }
    }

}

@Override
public Filter getFilter()
{
    Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {

            FilterResults filterResults = new FilterResults();
            synchronized (filterResults)
            {
                listTempPrefix.clear();
                listTemp.clear();
                //Log.e("1", "1");

                try {
                if(constraint != null) {
                    // A class that queries a web API, parses the data and returns an ArrayList<Style>
                    //StyleFetcher fetcher = new StyleFetcher();
                    //try {
                        //mData = fetcher.retrieveResults(constraint.toString());
                    //}
                    //catch(Exception e) {}
                    // Now assign the values and count to the FilterResults object


                    for(String value: mData) {
                        valueText = value.toLowerCase();

                        //System.out.println("constraintH - " + constraint);

                        constraint.toString().toLowerCase();
                        ulcase = constraint.toString().toLowerCase();
                        //System.out.println("ulcase - " + ulcase);

                        if (valueText.startsWith(ulcase)) {
                            listTempPrefix.add(value);
                        } else {
                            words = valueText.split(" ");
                            //final int wordCount = words.length;

                            // Start at index 0, in case valueText starts with space(s)
                            for (int k = 0; k < words.length; k++) {
                                if (words[k].startsWith(ulcase)) {
                                    listTemp.add(value);
                                    break;
                                }
                            }
                        }

                        ///listTemp.add(mData.get(i));
                        //filterResults.count = mData.size();
           //           System.out.println("mData" + i + mData.get(i));
                    }
                    //Log.e("2", "2");
           //       System.out.println("size " + listTemp.size() + " value" + listTemp);

                    listTempPrefix.addAll(listTemp);

                    filterResults.values = listTempPrefix;

                    filterResults.count = listTempPrefix.size();
                    //System.out.println("size " + filterResults.count + " value" + filterResults.values);

                    //System.out.println("constraint" + constraint);

                }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }
                return filterResults;
            }
        }

        @Override
        protected void publishResults(CharSequence contraint, FilterResults filterResults) 
        {
            synchronized (filterResults)
            {
                if(filterResults != null && filterResults.count > 0) {
                notifyDataSetChanged();
                //Log.e("notifyDataSetChanged", "notifyDataSetChanged");
                }
                else {
                    notifyDataSetInvalidated();
                    //Log.e("notifyDataSetInvalidated", "notifyDataSetInvalidated");
                }
            }
        }
    };
    return myFilter;
}

}

我有时得到的:请注意,这种情况很少发生。但我想完全摆脱这个错误。这是部分堆栈跟踪:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(-1, class android.widget.AutoCompleteTextView$DropDownListView) with Adapter(class com.example.test.AutoCompleteAdapter)].

问题可能是来自键盘的快速输入,方法 notifyDataSetChanged () 没有调用。但我不确定。

4

3 回答 3

11

将发布结果更改为

@Override 
protected void publishResults(final CharSequence contraint, final FilterResults filterResults) { 
    listTempPrefix = (List) results.values;
    if(filterResults != null && filterResults.count > 0) {
        notifyDataSetChanged();
    } else {
        notifyDataSetInvalidated();
    }
}

这样,GUI 线程会更新结果,而不是在后台线程中运行的 performFiltering。

从 performFiltering 中删除 listTempPrefix 引用并使用本地变量来存储结果并通过 FilterResults 返回它们

于 2013-11-18T20:01:43.997 回答
8

您正在更改listTempPrefix数组performFiltering(使用 clear 和 addAll),因此您需要调用notifyDataSetChanged以避免此异常。

但是performFiltering不会在 ui 线程上调用,所以调用notifyDataSetChanged也会引发异常。

解决这个问题最好的办法就是改变里面的listTempPrefix数组publishResults,然后调用notifyDataSetChanged

listTempPrefix删除对方法所做的更改performFiltering(您可能需要根据过滤器逻辑创建一个临时数组)。

在 上,使用 上包含的值publishResults更新您的数组,然后调用。listTempPrefixfilterResultsnotifyDataSetChanged

这是基于您的代码的示例:

@Override
public Filter getFilter()
{
    Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {

            FilterResults filterResults = new FilterResults();
            synchronized (filterResults)
            {
                //listTempPrefix.clear(); // Don't change listTempPrefix here
                 ...
                        ulcase = constraint.toString().toLowerCase();
                        //System.out.println("ulcase - " + ulcase);

                        if (valueText.startsWith(ulcase)) {
                            //listTempPrefix.add(value); // Don't change listTempPrefix
                            // To keep your logic you might need an aux array 
                            // for this part
                        } else {
                            ...
                        }

                    //listTempPrefix.addAll(listTemp); // Don't change it

                    filterResults.values = listTempPrefix; // No problem here

                    filterResults.count = listTempPrefix.size(); // No problem here
                    //System.out.println("size " + filterResults.count + " value" + filterResults.values);

                    //System.out.println("constraint" + constraint);

                }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }
                return filterResults;
            }
        }

        @Override
        protected void publishResults(CharSequence contraint, FilterResults filterResults) 
        {
            // At this point, make the changes you need to listTempPrefix
            // using filterResults.values
            synchronized (filterResults)
            {
                if(filterResults != null && filterResults.count > 0) {
                notifyDataSetChanged();
                //Log.e("notifyDataSetChanged", "notifyDataSetChanged");
                }
                else {
                    notifyDataSetInvalidated();
                    //Log.e("notifyDataSetInvalidated", "notifyDataSetInvalidated");
                }
            }
        }
    };
    return myFilter;
}
于 2012-10-31T11:44:52.520 回答
0

您必须在数据集( 、、等)支持您的更改时立即调用该notifyDataSetChanged()方法。否则你会得到这个。ArrayListArrayCursorAdapterException

于 2012-10-31T11:00:28.893 回答