如何在Android的ListView中搜索数据 [英] How to search data by ListView in Android
问题描述
我想通过的ListView
数据从哪里来一个websevice ..
描述:如果我输入的 [ABC] 搜索 EditView中[SEARCHTEXT]
如果我点击 serachbutton(buttonSearch )
,它必须显示 ListView中所有相关的数据(ListView控件)
例如:ABC,abacd,abcderg等这样...
这话题是新的我。
下面是我的code。请帮我做。
我都试过了,但我无法弄清楚如何做到这一点。
worklistview.java
的ArrayList<病人GT; patientListArray;
ListView控件=(ListView控件)findViewById(R.id.workListview);
objectAdapter =新WorkListAdapter(这一点,patientListArray);
listView.setAdapter(objectAdapter);
listView.setTextFilterEnabled(真); SEARCHTEXT =(EditText上)findViewById(R.id.searchView);
searchText.addTextChangedListener(新TextWatcher(){
公共无效afterTextChanged(编辑S){
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){
}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
。dataAdapter.getFilter()过滤(s.toString());
}
});WorkListAdapter.java公共过滤用getFilter(){ 过滤myFilter =新的过滤器(){ @覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){ 如果(!patientListArray.isEmpty()){
明确();
} 的for(int i = 0; I< patientListArray.size();我++){
如果(patientListArray.get(ⅰ).getName()。与toLowerCase()。startsWith(constraint.toString()。与toLowerCase())){
添加(patientListArray.get(I));
}
} notifyDataSetChanged();
} 私人无效添加(患者患者){
} 私人无效明确(){
} @覆盖
保护FilterResults performFiltering(CharSequence的约束){ 返回null;
}
}; 返回myFilter;
}
从图形上看,你需要用一个EditText(TextWatcher)和列表视图的布局。然后,你的列表视图的适配器将始终监听使用滤波器对你TextWatcher的变化(dataAdapter.getFilter()过滤(s.toString());)
更多细节这里
祝你好运。
I am trying to search data by ListView
data coming from a websevice..
Description: if I enter [abc] in search editview[searchText]
and if I click serachbutton(buttonSearch)
, it must show all related data in listview(listView)
Ex: abc, abacd, abcderg etc. in this way...
This topic is new for me.
Here is my code. Please help me do it.
I have tried, but I was not able to figure out how to do this.
worklistview.java
ArrayList<Patient> patientListArray;
listView = (ListView) findViewById(R.id.workListview);
objectAdapter = new WorkListAdapter(this, patientListArray);
listView.setAdapter(objectAdapter);
listView.setTextFilterEnabled(true);
searchText = (EditText) findViewById(R.id.searchView);
searchText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
WorkListAdapter.java
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (!patientListArray.isEmpty()){
clear();
}
for (int i = 0; i < patientListArray.size(); i++) {
if(patientListArray.get(i).getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
add(patientListArray.get(i));
}
}
notifyDataSetChanged();
}
private void add(Patient patient) {
}
private void clear() {
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return null;
}
};
return myFilter;
}
Graphically, you need a layout with an EditText (TextWatcher) and a listview. Then, the adapter of your listview will be always listening the changes on your TextWatcher using a filter ( dataAdapter.getFilter().filter(s.toString()); )
More details here
Good luck.
这篇关于如何在Android的ListView中搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!