Android在应用程序中添加自定义搜索栏 [英] Android add custom search bar in application

查看:218
本文介绍了Android在应用程序中添加自定义搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现搜索栏在我的应用程序,它会过滤列表视图取决于编辑的文本输入的字母。以下是我反倒像一个设计

I want to implement search bar in my application which will filter list view depending on letters entered in edit text. Here is what I have actually like a design

所以,我想通过标题,所以我想我必须要改变的sqlite的说法,因为我从填充数据库列表视图来筛选元素。

So I want to filter the elements by title so I guess I have to change the sqlite statement, because I'm populating the listview from database.

和我有两个问题:

1.Which是实现自定义搜索栏的最好方法:textwatcher添加到编辑文本,改变sqlite的语句每次用户输入字母或

1.Which is the best way to implement custom search bar : add textwatcher to an edit text and change the sqlite statement everytime user enter letter or ?

2.How在sqlite的排序,根据从用户输入的字母。如果用户preSS。我想表明它有这封信的所有项目无论是在开始或结束。

2.How to sort in sqlite depending on letters entered from user. If user press A I want to show all items which have that letter no matter if it's in the beginning or in the end.

推荐答案

所以,事情来实现这个很简单,无论你使用的是什么样的适配器的。首先,你需要创建 TextWatcher ,并把它添加到您的编辑文本。比你的 onAfterTextChanged()你可以这样建立新的SQL语句:

So the things to achieve this are very simple, no matter what kind of Adapter are you using. First you need to create TextWatcher and add it to your Edit Text. Than in your onAfterTextChanged() you can create your new SQL statement like :

String sql = "SELECT title FROM cards WHERE title LIKE '%"+someString+"%'"
//where someString is `Charsequence` which you get in your onAfterTextChanged()

在再次让你的数据,如果您使用的是的ArrayList 或任何其他形式的列表用于存储你的数据,你要清除它们,这样你就可以得到新的数据。而不是你可以简单地调用 adapter.notifySetDataChanged()。它应该工作。至少这是我怎么做的这种方式。也许这是不是最好的方式,但它的工作。

Before getting your data again if you are using ArrayList or any other kind of Lists for storing your data you have to clear them, so you can get the new data. And than you can simply call adapter.notifySetDataChanged(). And it should work. At least this is the way how I'm doing this. Maybe it's not the best way, but it's working.

这篇关于Android在应用程序中添加自定义搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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