如何使用编辑文本作为可扩展的ListView android的搜索框? [英] How to use edit text as search box in expandable listview android?
问题描述
在我的应用我使用view.Now我想使用搜索框显示过滤的扩展列表视图items.For这个目的,我现在用的是下面的code扩展列表
搜索=(搜索查看)findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(假);
search.setOnQueryTextListener(本);
search.setOnCloseListener(本);
不过,这种编码只支持上述API 11.But我要实现这些功能在下面的API 11。
这是使用编辑文本作为默认的列表视图的适配器搜索视图
之路 inputSearch.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
//当用户更改文本
。MainActivity.this.adapter.getFilter()过滤器(CS);
}
@覆盖
公共无效beforeTextChanged(CharSequence的arg0中,诠释ARG1,INT ARG2,
INT ARG3){
// TODO自动生成方法存根
}
@覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
我所做的是,为什么实施了搜索我的数据我自己。
我添加一个TextView的动作条,我处理输入我的ListAdapter。
你的目标低于11 API,您将不得不增加ActionBarSherlock,或将TextView的其他地方。
的EditText电视=新的EditText(本);
tv.setOnEditorActionListener(新OnEditorActionListener(){
@覆盖
公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
如果(event.getAction()== KeyEvent.KEY code_ENTER){
yourAdapter.filterData(v.getText());
返回true;
}
返回false;
}
});
这是我怎么会设计一个TextView来处理搜索。你将不得不执行搜索自己,因为我的数据是由一个SQLite数据库的支持,所以我刚刚交班搜索到的SQL数据库。
公共无效筛选数据(查询字符串){
查询= query.toLowerCase();
Log.v(MyListAdapter,将String.valueOf(continentList.size()));
continentList.clear();
如果(query.isEmpty()){
continentList.addAll(originalList);
}
其他 {
对于(大陆大陆:originalList){
ArrayList的<国家> countryList = continent.getCountryList();
ArrayList的<国家> newList =新的ArrayList<国家>();
对于(国家国家:countryList){
如果(country.get code()。与toLowerCase()。包括(查询)||
country.getName()。与toLowerCase()。包含(查询)){
newList.add(国家);
}
}
如果(newList.size()大于0){
大陆nContinent =新大洲(continent.getName(),newList);
continentList.add(nContinent);
}
}
}
Log.v(MyListAdapter,将String.valueOf(continentList.size()));
notifyDataSetChanged();
}
您将不得不更新搜索方法,以适应您的数据。
In my application i am using expandable list view.Now i want to use search box for display the filtered expandable list view items.For this purpose i am using the following code
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
But this coding only supports above API 11.But i want to implement these feature in below API 11.
This is the way of using edit text as search view for the default list view adapter
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
What i did is, why implemented a search for my Data myself.
i added a TextView to the Actionbar and i handle the input in my ListAdapter.
as you are targeting api below 11 you will either have to add ActionBarSherlock, or place the TextView elsewhere.
EditText tv = new EditText(this);
tv.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.KEYCODE_ENTER) {
yourAdapter.filterData(v.getText());
return true;
}
return false;
}
});
this is how i would design a textView to handle a search. you will have to implement the search yourself, because my data is backed by an sqlite database so i just hand off the search to the sql database.
public void filterData(String query){
query = query.toLowerCase();
Log.v("MyListAdapter", String.valueOf(continentList.size()));
continentList.clear();
if(query.isEmpty()){
continentList.addAll(originalList);
}
else {
for(Continent continent: originalList){
ArrayList<Country> countryList = continent.getCountryList();
ArrayList<Country> newList = new ArrayList<Country>();
for(Country country: countryList){
if(country.getCode().toLowerCase().contains(query) ||
country.getName().toLowerCase().contains(query)){
newList.add(country);
}
}
if(newList.size() > 0){
Continent nContinent = new Continent(continent.getName(),newList);
continentList.add(nContinent);
}
}
}
Log.v("MyListAdapter", String.valueOf(continentList.size()));
notifyDataSetChanged();
}
you would have to update the search method to fit your data.
这篇关于如何使用编辑文本作为可扩展的ListView android的搜索框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!