如何在android的自定义listview中实现搜索? [英] how to implement search in custom listview in android?
本文介绍了如何在android的自定义listview中实现搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中有一个编辑文本和一个列表视图,我的列表视图显示联系人列表.我想要带有编辑文本的列表视图过滤器.我在谷歌上搜索了很多,找到了一些例子,但没有一个对我有用,这是我的代码
我的自定义适配器
I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles but none worked for me here's my code
my custom adapter
public class ContactListAdapter extends ArrayAdapter {
private final Activity activity;
private final List<ContactStock> stocks;
private ArrayList<ContactStock> arraylist;
public ContactListAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.listview_detail, objects);
this.activity = activity;
this.stocks = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
ContactStockView sv = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(
R.layout.listview_detail, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sv = new ContactStockView();
sv.name = (TextView) rowView.findViewById(R.id.textView1);
sv.number = (TextView) rowView.findViewById(R.id.textView2);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sv);
} else {
sv = (ContactStockView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
ContactStock currentStock = (ContactStock) stocks.get(position);
sv.name.setText(currentStock.getName());
sv.number.setText(currentStock.getNumber());
// TODO Auto-generated method stub
return rowView;
}
protected static class ContactStockView {
protected TextView name;
protected TextView number;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
stocks.clear();
if (charText.length() == 0) {
stocks.addAll(arraylist);
} else {
for (ContactStock cs : arraylist) {
if (cs.getName().contains(charText)) {
stocks.add(cs);
}
}
}
notifyDataSetChanged();
}
}
主类edittext代码为
Main class edittext code is
edittext = (EditText)findViewById(R.id.editText1);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//MainActivity.this.adapt.getFilter().filter(s);
String searchString=edittext.getText().toString();
adapt.filter(searchString);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
没有自定义适配器,它可以与 getfilter() 一起使用.但我不知道如何使用自定义适配器进行过滤.任何帮助都会得到帮助.提前致谢
without custom adapter it is working with getfilter(). But I don't know how to filter with custom adapter. any help will be appriciated. thanks in advance
推荐答案
这个对我有用:
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
int textlength = cs.length();
ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
for(ContactStock c: arraylist){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
tempArrayList.add(c);
}
}
}
mAdapter = new ContactListAdapter(activity, tempArrayList);
lv.setAdapter(mAdapter);
}
这篇关于如何在android的自定义listview中实现搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文