listview分页(上一页,下一页效果)并支持过滤(二)
程序员文章站
2022-05-17 09:50:09
...
上一篇文章中,只对listview进行了分页,本文将在上一篇代码的基础上,增加过滤的功能,过滤首先要给listview增加一个属性android:textFilterEnabled="true",然后BaseAdapter要继承Filterable的接口。废话少说,上代码:
DsznzActivity代码,大部分代码和上一篇文章中的代码相同,就不重复写了,只写有改动和增加的
//增加了两个属性 private ArrayList<HashMap<String, String>> tmpListItem; private EditText inputSearch; MyAdapter myAdapter; inputSearch=(EditText) findViewById(R.id.inputSearch); inputSearch.addTextChangedListener(new TextWatcher() { //监听文本框的改变 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { myAdapter.getFilter().filter(s); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { } @Override public void afterTextChanged(Editable s) { } }); //tmpListItem是用来存储从数据库中获取到的所有记录,当listItem发生变化时,可以从tmpListItem中过滤取得需要的值 tmpListItem = new ArrayList<HashMap<String, String>>(); //在给listItem赋值的同时,也给tmpListItem赋值 JSONArray array = new JSONArray(URLDecoder.decode(json, "utf-8")); for (int i = 0; i < array.length(); i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("ylfn_did", array.getJSONObject(i).getString("did")); map.put("ylfn_name", array.getJSONObject(i).getString("name")); map.put("gmsfz", array.getJSONObject(i).getString("gmsfz")); listItem.add(map); tmpListItem.add(map); } //MyAdapter // ListView的Adapter,这个是关键的导致可以分页的根本原因。 public class MyAdapter extends BaseAdapter implements Filterable{ Activity activity; private MyFilter filter; public MyAdapter(Activity a) { activity = a; } // 设置每一页的长度,默认的是View_Count的值。 @Override public int getCount() { // TODO Auto-generated method stub // return data.length; // ori表示到目前为止的前几页的总共的个数。 int ori = VIEW_COUNT * index; // 值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了 if (listItem.size() - ori < VIEW_COUNT) { return listItem.size() - ori; } // 如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。 else { return VIEW_COUNT; } } @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 // return addTestView(position); convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.ylfn,null); TextView ylfn_did_view = (TextView)convertView.findViewById(R.id.ylfn_did); TextView ylfn_name_view = (TextView)convertView.findViewById(R.id.ylfn_name); TextView ylfn_gmsfz_view = (TextView)convertView.findViewById(R.id.gmsfz); ylfn_did_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_did")); ylfn_name_view.setText(listItem.get(position + index * VIEW_COUNT).get("ylfn_name")); ylfn_gmsfz_view.setText(listItem.get(position + index * VIEW_COUNT).get("gmsfz")); return convertView; } @Override public Filter getFilter() { if(filter == null){ filter = new MyFilter(); } return filter; } } //MyFilter private class MyFilter extends Filter{ @Override protected FilterResults performFiltering(CharSequence constraint) { //存储过滤的值 FilterResults retval = new FilterResults(); retval.values = tmpListItem; retval.count = tmpListItem.size(); //有筛选条件时,显示被筛选的内容; 没有筛选条件时,显示所有内容 if(constraint != null && constraint.toString().length() > 0) { constraint = constraint.toString().toUpperCase(); ArrayList<HashMap<String, String>> filt = new ArrayList<HashMap<String, String>>(); for(int i = 0; i < tmpListItem.size(); i++) { HashMap<String, String> ylfn_i = tmpListItem.get(i); if( ylfn_i.get("ylfn_did").toUpperCase().contains(constraint) || ylfn_i.get("ylfn_name").toUpperCase().contains(constraint) || ylfn_i.get("gmsfz").toUpperCase().contains(constraint)) { filt.add(ylfn_i); } } retval.count = filt.size(); retval.values = filt; } return retval; } @Override protected void publishResults(CharSequence constraint, FilterResults retval) { //先清空旧数据 listItem.clear(); myAdapter.notifyDataSetChanged(); //再添加新数据 index=0; //过滤后重新分页,当前为第一页 listItem.addAll((ArrayList<HashMap<String, String>>)retval.values); myAdapter.notifyDataSetChanged(); checkButton(); } }