这几天公司提出了一需求,要做一个android抢单的app,其中需要一个搜索功能,研究了一阵,现在记录一下。界面xml代码,注意其中的searchView控件和ListView控件,注意:searchView需要api至少是android3.0
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_tab_weixin" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffefebe7" android:orientation="vertical" > <RelativeLayout android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@drawable/title_bar" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="可抢工单" android:textColor="#ffffff" android:textSize="20sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/robjob_line" > <Button android:id="@+id/bt_search_condition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginTop="3dp" android:layout_weight="0" android:text="搜索条件" /> <Button android:id="@+id/bt_Refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginTop="3dp" android:layout_weight="0" android:text="刷新" /> <SearchView android:id="@+id/search_view" style="@style/my_edittext_style" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:iconifiedByDefault="false" android:inputType="textFilter" android:queryHint="搜索项目名称" > </SearchView> </LinearLayout> <ListView android:id="@+id/lv_robjob" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffefebe7" android:divider="@null" > </ListView> </LinearLayout>接下来将是主要的代码,但是限于仅处于开发初始阶段,所以数据都是模拟的,这篇文章主要介绍的也不是处理数据,请大家见谅。
public class RobJobFragment extends Fragment implements OnQueryTextListener { private View mMainView; private ListView listView; private SearchView searchView; private Button bt_search_condition, bt_Refresh; private List<String[]> list; private RobJobListAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = getActivity().getLayoutInflater(); mMainView = inflater.inflate(R.layout.main_tab_robjob, (ViewGroup) getActivity().findViewById(R.id.tabpager), false); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { searchView = (SearchView) mMainView.findViewById(R.id.search_view); bt_search_condition = (Button) mMainView .findViewById(R.id.bt_search_condition); bt_Refresh = (Button) mMainView.findViewById(R.id.bt_Refresh); //刷新列表的方法 bt_Refresh.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { list.clear(); list.add(new String[] { "1", "监管报送系统", "需求分析", "银行监管报送系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "3个月", "2000元", "2014-02-28","18643328574" }); list.add(new String[] { "2", "绩效考核系统", "详细设计", "绩效考核系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "6个月", "3000元", "2014-03-01" ,"18744993322"}); list.add(new String[] { "3", "濮阳征信系统", "概要设计", "濮阳征信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "4个月", "30000元", "2014-03-01" ,"16574329975"}); list.add(new String[] { "4", "内部银行系统", "操作拓扑", "内部银行系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "5个月", "15000元", "2014-03-01" ,"13955892134"}); list.add(new String[] { "5", "银行卡部系统", "业务流程", "银行卡部系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "7个月", "2500元", "2014-03-01" ,"14788993743"}); list.add(new String[] { "6", "濮阳授信系统", "组件化拓扑", "濮阳授信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "9个月", "1000元", "2014-03-01" ,"18064958732"}); list.add(new String[] { "7", "濮阳征信系统1", "概要设计", "濮阳征信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "4个月", "30000元", "2014-03-01","13555857643" }); list.add(new String[] { "8", "内部银行系统1", "操作拓扑", "内部银行系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "5个月", "5000元", "2014-03-01" ,"13677894323"}); list.add(new String[] { "9", "银行卡部系统1", "业务流程", "银行卡部系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "7个月", "20000元", "2014-03-01" ,"18940912691"}); list.add(new String[] { "10", "濮阳授信系统1", "组件化拓扑", "濮阳授信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "9个月", "400元", "2014-03-01","13999674836" }); adapter.setList(list); MainActivity m = (MainActivity) getActivity(); m.setSort("全部"); m.setLimitTime("全部"); m.setPrice("全部"); adapter.notifyDataSetChanged(); } }); bt_search_condition.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getActivity(), SearchCondition.class); getActivity().startActivity(intent); } }); listView = (ListView) mMainView.findViewById(R.id.lv_robjob); list = new ArrayList<String[]>(); list.add(new String[] { "1", "监管报送系统", "需求分析", "银行监管报送系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "3个月", "2000元", "2014-02-28","18643328574" }); list.add(new String[] { "2", "绩效考核系统", "详细设计", "绩效考核系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "6个月", "3000元", "2014-03-01" ,"18744993322"}); list.add(new String[] { "3", "濮阳征信系统", "概要设计", "濮阳征信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "4个月", "30000元", "2014-03-01" ,"16574329975"}); list.add(new String[] { "4", "内部银行系统", "操作拓扑", "内部银行系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "5个月", "15000元", "2014-03-01" ,"13955892134"}); list.add(new String[] { "5", "银行卡部系统", "业务流程", "银行卡部系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "7个月", "2500元", "2014-03-01" ,"14788993743"}); list.add(new String[] { "6", "濮阳授信系统", "组件化拓扑", "濮阳授信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "9个月", "1000元", "2014-03-01" ,"18064958732"}); list.add(new String[] { "7", "濮阳征信系统1", "概要设计", "濮阳征信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "4个月", "30000元", "2014-03-01","13555857643" }); list.add(new String[] { "8", "内部银行系统1", "操作拓扑", "内部银行系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "5个月", "5000元", "2014-03-01" ,"13677894323"}); list.add(new String[] { "9", "银行卡部系统1", "业务流程", "银行卡部系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "7个月", "20000元", "2014-03-01" ,"18940912691"}); list.add(new String[] { "10", "濮阳授信系统1", "组件化拓扑", "濮阳授信系统,含有:\n1104、集中上报、反洗钱、\n利率分析、企业报表", "9个月", "400元", "2014-03-01","13999674836" }); list = getSearchResult(); adapter = new RobJobListAdapter(getActivity()); adapter.setList(list); listView.setAdapter(adapter); listView.setTextFilterEnabled(true); searchView.setOnQueryTextListener(this); searchView.setSubmitButtonEnabled(false); ViewGroup p = (ViewGroup) mMainView.getParent(); if (p != null) { p.removeAllViewsInLayout(); Log.v("huahua", "fragment1-->移除已存在的View"); } return mMainView; } public String getSort() { MainActivity m = (MainActivity) getActivity(); return m.getSort(); } public String getLimitTime() { MainActivity m = (MainActivity) getActivity(); return m.getLimitTime(); } public String getPrice() { MainActivity m = (MainActivity) getActivity(); return m.getPrice(); } /** * 获取搜索条件,并对list进行搜索 * */ public List<String[]> getSearchResult() { List<String[]> resultList = new ArrayList<String[]>(); for (int i = 0; i < list.size(); i++) { if (list.get(i)[2].equals(getSort()) || getSort().equals("全部")) { if (list.get(i)[4].equals(getLimitTime()) || getLimitTime().equals("全部")) { System.out.println("-----截取数据----->" + getPrice().substring(0, getPrice().length() - 1)); if (getPrice().equals("全部") || Integer.parseInt(list.get(i)[5].substring(0, list.get(i)[5].length() - 1)) <= Integer .parseInt(getPrice().substring(2))) { resultList.add(list.get(i)); } } } } return resultList; } @Override public boolean onQueryTextSubmit(String newText) { return false; } /** * 为listView设置一个过滤器,将searchView的文字以参数传进来 * */ @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { // Clear the text filter. listView.clearTextFilter(); } else { // Sets the initial value for the text filter. listView.setFilterText(newText.toString()); } return false; } }注意上面的代码,该类实现了OnQueryTextListener接口,并复写onQueryTextSubmit和onQueryTextChange方法。
接下来,就是关键中的关键,自定义的适配器,需要实现Fileterable接口,复写getFilter方法,需要构造一个继承于Filter的类,在performFiltering方法中书写关于搜索的算法。
publishResults方法是把根据规则筛选出来的数据更新到list中,并刷新listView。
public class RobJobListAdapter extends BaseAdapter implements Filterable { List<String[]> list = null; Context context = null; private Filter filter; public RobJobListAdapter(Context context) { this.context = context; } public void setList(List<String[]> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position)[1]; } @Override public long getItemId(int position) { return Integer.parseInt(list.get(position)[0].trim()); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { view = LayoutInflater.from(context).inflate( R.layout.robjob_list_style, null); } else { view = convertView; } TextView description = (TextView) view.findViewById(R.id.description); TextView type = (TextView) view.findViewById(R.id.type); TextView price = (TextView)view.findViewById(R.id.tv_price); Button details = (Button) view.findViewById(R.id.bt_details); description.setText(list.get(position)[1]); type.setText(list.get(position)[2]); price.setText(list.get(position)[5]); details.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("details", list.get(position)); intent.setClass(context, RobJobDetail.class); context.startActivity(intent); } }); return view; } @Override public Filter getFilter() { if (filter == null) { filter = new ProjectNameFilter(list); } return filter; } //项目名称过滤器 private class ProjectNameFilter extends Filter { private List<String[]> original; public ProjectNameFilter(List<String[]> list) { this.original = list; } @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); if (constraint == null || constraint.length() == 0) { results.values = original; results.count = original.size(); } else { List<String[]> mList = new ArrayList<String[]>(); for (String[] p : original) { if (p[1].toUpperCase().startsWith( constraint.toString().toUpperCase())) { mList.add(p); } } results.values = mList; results.count = mList.size(); } return results; } /** *更新list列表 * */ @Override protected void publishResults(CharSequence constraint, FilterResults results) { list = (List<String[]>) results.values; notifyDataSetChanged(); } } }
源码下载地址:http://download.csdn.net/detail/xf396191344/7308343