Java 自定义阵列适配器的ListActivity筛选器

Java 自定义阵列适配器的ListActivity筛选器,java,android,Java,Android,我读过几篇关于ListActivity使用简单过滤器的教程,但我就是不明白为什么它对我不起作用。TextWatcher的onTextChanged()方法是使用要搜索的正确字符串执行的……但随后什么也没有发生。我认为问题可能在于自定义适配器,但如何使其工作 也许你可以看看:) 谢谢 package.com.RemoteControl; 导入com.RemoteControl.R; 导入android.app.ListActivity; 导入android.content.Context; 导入a

我读过几篇关于ListActivity使用简单过滤器的教程,但我就是不明白为什么它对我不起作用。TextWatcher的onTextChanged()方法是使用要搜索的正确字符串执行的……但随后什么也没有发生。我认为问题可能在于自定义适配器,但如何使其工作

也许你可以看看:) 谢谢

package.com.RemoteControl;
导入com.RemoteControl.R;
导入android.app.ListActivity;
导入android.content.Context;
导入android.os.Bundle;
导入android.os.Handler;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类RemoteControlPlaylist扩展了ListActivity{
静态处理程序错误\u类\u处理程序;
静态处理程序viewHandler;
静态效率适配器=空;
私有静态EditText filterText=null;
静态类EfficientAdapter扩展了ArrayAdapter{
public EfficientAdapter(上下文上下文、int-textViewResourceId、int-playlityTitle、字符串[]对象){
超级(上下文、textViewResourceId、播放列表标题、对象);
mInflater=LayoutInflater.from(上下文);
}
私人停车场;
@凌驾
public int getCount(){
返回设置。播放长度;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null)
{
holder=新的ViewHolder();
convertView=mInflater.充气(R.layout.listview,null);
holder.text=(TextView)convertView.findViewById(R.string.playlist\u title);
holder.image=(ImageView)convertView.findViewById(R.string.playlist\u play);
holder.queue=(TextView)convertView.findViewById(R.string.playlist\u queue);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
返回视图;
}
静态类视窗夹{
文本查看文本,队列;
图像视图图像;
}
@凌驾
公共字符串getItem(int位置){
返回设置。播放列表[位置];
}
}
私有静态TextWatcher filterTextWatcher=新TextWatcher(){
公共无效后文本已更改(可编辑){
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
public void onTextChanged(字符序列,int start,int before,
整数计数){
adapter.getFilter().filter;
}
};
void initialize()
{
adapter=new EfficientAdapter(这个,R.layout.listview,R.string.playlist\u title,Settings.playlist);
setListAdapter(适配器);
filterText=(EditText)findViewById(R.string.search\u框);
filterText.addTextChangedListener(filterTextWatcher);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
getListView().setTextFilterEnabled(true);
初始化();
错误_class_Handler=new Handler();
viewHandler=新处理程序();
getListView().setFastScrollEnabled(true);
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
filterText.removeTextChangedListener(filterTextWatcher);
}
}
其中playlist.xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <EditText android:id="@+string/search_box" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="type to filter"
       android:inputType="text"
       android:maxLines="1"/>

    <ListView android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/>

</LinearLayout>

ListView中的每一行都是ListView.xml元素:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


     <TextView
            android:id="@+string/playlist_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            <!-- some layout stuff --> />

    <ImageView 
        android:id="@+string/playlist_play"
        android:src="@drawable/playlist_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            <!-- some layout stuff --> />

    <TextView
        android:id="@+string/playlist_queue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            <!-- some layout stuff --> />


</RelativeLayout>

/>

我认为来自数组适配器的标准过滤器只过滤那些在构造函数中传递的
列表(数组)对象。但您重写了方法
getCount
getItem
等,所以适配器不使用您在构造函数中传递的列表。但是当您调用
getFilter().filter
时,它会过滤此列表

因此您有了类
EfficientAdapter
,它扩展了
ArrayAdapter
。 创建
EfficientAdapter
时,也会创建并初始化ArrayAdapter的部件。在构造函数中,您传递了字符串数组,ArrayAdapter的部分存储它们。标准过滤器将过滤这些内容,而不是您的设置。播放列表列表。 您可以做什么-不要使用Settings.playlist(也不要覆盖
getItem
…),而只使用您在构造函数中传递的列表。我想这样应该行得通

内部ArrayAdapter具有以下字段:

/**
 * Contains the list of objects that represent the data of this ArrayAdapter.
 * The content of this list is referred to as "the array" in the documentation.
 */
private List<T> mObjects;

private ArrayList<T> mOriginalValues;
/**
*包含表示此ArrayAdapter数据的对象列表。
*此列表的内容在文档中称为“数组”。
*/
私有列表对象;
私人ArrayList mOriginalValues;
内部ArrayAdapter的过滤器—
ArrayFilter
,过滤
mOriginalValues
,并添加所有匹配的值,这些值保留在
mObjects
,然后ArrayAdapter“显示”
mObjects

请参阅
ArrayAdapter
(和
ArrayFilter
)以更好地了解
getFilter().filter
在您的案例中的作用。

阅读课堂上的内容,学习Android开发人员解决此问题的方法

ArrayFilter
是一个自定义的
Filter
类,其实例由方法使用并返回。如果你仔细阅读,我相信你会得到所有答案。

啊,非常感谢!:)过滤工作正常,但现在我在调用adapter.clear()时总是收到一个UnsupportedOperationException
/**
 * Contains the list of objects that represent the data of this ArrayAdapter.
 * The content of this list is referred to as "the array" in the documentation.
 */
private List<T> mObjects;

private ArrayList<T> mOriginalValues;