在Android的ListView的简单搜索过滤 [英] Simple search filtering in Android listView
问题描述
我想在我的Android ListView控件添加简单的搜索过滤器,就像这里:
Search.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,
INT ARG3){ 。MainActivity.this.adapter.getFilter()过滤器(CS);
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){ } @覆盖
公共无效afterTextChanged(编辑为arg0){ }
});
这code是从互联网上找到的教程(我已经失去了一个URL)。它是基于ArrayAdapter,但在我的课我使用SimpleCursorAdapter这code不起作用。我的意思是我可以输入,但它不会过滤搜索结果。这里是我的code:
//创建指向所需的数据
DataAdapter的=新SimpleCursorAdapter(
此,R.layout.contact_row,contactsCursor,列,向,0);
ListView控件的ListView =(ListView控件)findViewById(R.id.list);
搜索=(EditText上)findViewById(R.id.inputSearch); Search.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,
INT ARG3){ 。ContactListActivity.this.dataAdapter.getFilter()过滤器(CS);
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){ } @覆盖
公共无效afterTextChanged(编辑为arg0){ }
});
有什么办法来设置SimpleCursorAdapter此过滤器功能?
Search.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,
INT ARG3){
字符串,其中* = * YOURCOLUMN +LIKE'%+ CS +%'; 光标C = db.query(*表,*柱*,其中,NULL,NULL,NULL,NULL); dataAdapter.swapCursor(C);
dataAdapter.notifyDataSetChanged();
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){ } @覆盖
公共无效afterTextChanged(编辑为arg0){ }
});
显然,你要去需要把自己的数据,但在大多数情况下应该做你想要什么。希望它的作品了!
I want to add simple search filter in my Android ListView just like here :
Search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
This code is from tutorial found in the internet (I have lost an url). It is based on ArrayAdapter but in my class I am using SimpleCursorAdapter and this code doesn't work. I mean I can type but it won't filter search results. Here is my code :
// Create the pointer to the desired data
dataAdapter = new SimpleCursorAdapter(
this, R.layout.contact_row, contactsCursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.list);
Search = (EditText) findViewById(R.id.inputSearch);
Search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
ContactListActivity.this.dataAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
Is there any way to set this filter function on SimpleCursorAdapter?
Search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
String where = *YOURCOLUMN* + " LIKE '%" + cs + "%'";
Cursor c = db.query(*TABLE*, *COLUMNS*, where, null, null, null, null);
dataAdapter.swapCursor(c);
dataAdapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
Obviously your gonna need to put in your own data, but for the most part that should do what you want. Hope it works out!
这篇关于在Android的ListView的简单搜索过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!