ArrayAdapter和android:textFilterEnabled [英] ArrayAdapter and android:textFilterEnabled

查看:269
本文介绍了ArrayAdapter和android:textFilterEnabled的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个布局,其中包括一个ListView。

I have created a layout which includes a ListView.

这ListView控件中显示的数据是不是太复杂,它主要是在扩展活动开始被传递的数组。

The data shown within this ListView isn't too complicated, it's mostly an Array which is passed when the extended Activity is started.

本身存在了一个图标和文本,从而一个的ImageView和一个TextView的行。 为了填补起来,我用简单,因为传递一个Array包含所有文字项应显示一个ArrayAdapter ListView控件。

The rows themselves exist out of an icon and a text, thus an ImageView and a TextView. To fill up the ListView I use an ArrayAdapter simply because an Array is passed containing all the text-items that should be shown.

现在我想居然能过滤那些,所以我发现了android:textFilterEnabled paramater上添加ListView的XML声明... 现在搜索领域是很好的显示,但是当我输入一些字母,将不过滤,但它会直接删除整个列表。 我发现,那是因为textfilter不知道什么应该过滤。

Now I'd like to actually be able to filter those, thus I found the android:textFilterEnabled paramater to add on the ListView xml declaration... Now a search field is shown nicely but when I enter some letters it won't filter but it will simply delete the whole list. I found out that that's because the textfilter has no idea what it should filter.

所以,现在我的问题是:我知道我需要告诉textfilter它应该过滤,我也还是我的数组充满了应该得到过滤,使文本我如何夫妇这两个

So now my question is : I know I need to tell the textfilter what it should filter, I also still have my array filled with the text that should get filtered so how do i couple those two?

我有延伸的CursorAdapter看到的例子,但同样,我没有一个光标,我不想做调用一个DB我想和数据再利用我的阵列,显然一个ArrayAdapter本身,以便数据会重新在屏幕上体面psented $ P $(即我的ImageView和TextView的布局)。

I have seen examples extending a CursorAdapter, but again, I don't have a Cursor, I don't want to do calls to a DB I want to re-utilize my Array with data and obviously the ArrayAdapter itself so that the data will be represented decently on screen (i.e with my ImageView and TextView layout).

如何才能做到这一点?

推荐答案

您自定义适配器必须实现可筛选接口,覆盖用getFilter() 用getFilter返回一个类,扩展了过滤器的一个实例()

Your custom adapter has to implement the Filterable interface, override getFilter() and return an instance of a class which extends Filter in getFilter().

我做的方式是创建过滤类的实例,并将其存储在我的适配器在最后实例变量并返回,当用getFilter()之称。

The way I did it was to create an instance of the filtering class and store it in a final instance variable inside my adapter and return that when getFilter() is called.

有一个看 ArrayAdapter 源$ C ​​$ C以获得一个更好地了解如何做过滤吧。

Have a look at the ArrayAdapter source code to get a better idea of how to do the filtering yourself.

这篇关于ArrayAdapter和android:textFilterEnabled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆