Android 筛选时未更新自定义ArrayAdapter
我已经从实现了CustomArrayAdapter,它有一个拇指指甲,列表视图的每个条目有两行。除了过滤功能外,它工作得非常好。我一直在尝试实现以下过滤和问题, 但是listview从未更新过 这是我的多行布局代码:Android 筛选时未更新自定义ArrayAdapter,android,android-listview,listadapter,Android,Android Listview,Listadapter,我已经从实现了CustomArrayAdapter,它有一个拇指指甲,列表视图的每个条目有两行。除了过滤功能外,它工作得非常好。我一直在尝试实现以下过滤和问题, 但是listview从未更新过 这是我的多行布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/thumb"
android:background="@drawable/thumb"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/custom_title"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="@color/black"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/desc"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="@color/darkgray"
android:textStyle="italic"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
(不是100%确定R.id.custom\u title
是我应该放在那里的id
我添加了调试消息,正在调用TextChange,作为getFilter()方法。
所以我不知道我做错了什么。在我从ListAdapter切换到CustomListAdapter之前,过滤工作正常:(
感谢您的帮助未过滤
列表视图
,因为您在适配器类中未使用的子项
上设置了过滤结果,因此对notifyDataSetChanged
的调用将无法工作,因为从适配器的角度来看,数据是完整的。您可以使用另一个列表来保存旧列表值,它们将是必需的,这样您就有了所有的旧值。要解决此问题,请添加行:
subItems = new ArrayList<CustomListItem>(searchArrayList);
然后在performFiltering
方法中:
if (prefix!= null && prefix.toString().length() > 0) {
// use the initial values !!!
for (int index = 0; index < subItems.size(); index++) {
CustomListItem si = subItems.get(index);
final int length = prefix.length();
// if you compare the Strings like you did it will never work as you compare the full item string(you'll have a match only when you write the EXACT word)
// keep in mind that you take in consideration capital letters!
if(si.toString().substring(0, prefixLength).compareTo(prefix.toString()) == 0){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
// revert to the old values
synchronized (searchArrayList){
results.values = subItems;
results.count = subItems.size();
}
}
if(前缀!=null&&prefix.toString().length()>0){
//使用初始值!!!
对于(int index=0;index
我希望它能起作用
(不是100%确定R.id.custom_title是我应该放置的id
在那里
这与您使用自己的自定义适配器无关。您是否覆盖了
CustomListItem
类的toString
方法以返回有意义的内容?是的,我返回了title+description
subItems = new ArrayList<CustomListItem>(searchArrayList);
searchArrayList = (ArrayList<CustomListItem>)results.values;
notifyDataSetChanged();
if (prefix!= null && prefix.toString().length() > 0) {
// use the initial values !!!
for (int index = 0; index < subItems.size(); index++) {
CustomListItem si = subItems.get(index);
final int length = prefix.length();
// if you compare the Strings like you did it will never work as you compare the full item string(you'll have a match only when you write the EXACT word)
// keep in mind that you take in consideration capital letters!
if(si.toString().substring(0, prefixLength).compareTo(prefix.toString()) == 0){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
// revert to the old values
synchronized (searchArrayList){
results.values = subItems;
results.count = subItems.size();
}
}