listview+EditText 实现过滤搜索listview中的内容

时间:2022-08-07 19:38:11

废话不说直接上代码   

public class ListViewAdapter extends BaseAdapter {
//集合的总数据
private List<People> data;
private LayoutInflater inflater;
//过滤的数据
private List<People> filterList;
//把这个数据给adapter
private List<People> myPeopleList;

public ListViewAdapter(Context context, List<People> data){
this.inflater=LayoutInflater.from(context);
this.data=data;
//这个时候把我们的一些中间变量给初始化一下
filterList = new ArrayList<People>();
myPeopleList = new ArrayList<People>();
//把我们传递过来的数据,弄到这个集合里面.保存原始集合的完整性
myPeopleList.addAll(data);


}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
int type=getItemViewType(position);
if(convertView==null){
holder=new ViewHolder();

convertView=inflater.inflate(R.layout.item1, null);
holder.tv=(TextView) convertView.findViewById(R.id.tv1);

convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}

return convertView;
}
class ViewHolder{
TextView tv;
}
//定义自己的过滤器
private MyFilter myfilter;
public MyFilter getFilter(){
if(myfilter==null){
myfilter=new MyFilter();
}
return myfilter;
}

class MyFilter extends Filter{

@Override
protected FilterResults performFiltering(CharSequence charSequence) {
// TODO Auto-generated method stub
FilterResults results=new FilterResults();
if(TextUtils.isEmpty(charSequence)){
filterList.clear();
results.values=data;
return results;
}else{
filterList.clear();
for(Iterator<People> iterator=data.iterator();iterator.hasNext();){
People name = iterator.next();
if (name.getName().contains(charSequence)) {
filterList.add(name);
}
}
results.values = filterList;
return results;
}
}
//筛选结果
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence arg0, FilterResults arg1) {
// TODO Auto-generated method stub
myPeopleList=(List<People>) arg1.values;
if (arg1.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}

}

}
下面是Main_Activity 

public class MainActivity extends Activity {
private ListView lv;
private ListViewAdapter adapter;
private List<People> list;

private EditText ed;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);
lv=(ListView) findViewById(R.id.lv);
ed=(EditText) findViewById(R.id.et);
//mData=getData();

list = new ArrayList<People>();
for(int i=1;i<20;i++) {
People tPeople = new People();
if(i==18||i==3||i==2){
tPeople.setName("小明"+i);
}else{
tPeople.setName("小刚"+i);
}
list.add(tPeople);
}



adapter=new ListViewAdapter(this, list);
lv.setAdapter(adapter);
ed.addTextChangedListener(new TextWatcher() {

@Override//当editText发生变化的时候
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
adapter.getFilter().filter(arg0);
}
//这两个方法在这里没有什么用
@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

}
});

}




}

哈哈    简单吧   这是部分代码  。。