• Android利用Filter过滤数据


    MainActivity如下:

    package cc.testfilterable;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.app.Activity;
    import android.content.Context;
    /**
     * Demo描述:
     * 利用Filter过滤数据
     * 可用于AutoCompleteTextView筛选数据等功能
     * 
     * 备注说明:
     * 该Demo只表述原理,代码逻辑较简单,有一些小的bug.
     * 完整的应用请参见以下资料.
     * 
     * 参考资料:
     * 1 http://gundumw100.iteye.com/blog/1446507
     * 2 http://blog.csdn.net/ssstudio/article/details/7579089
     * 3 http://blog.csdn.net/jiahui524/article/details/7802033
     * 
     */
    public class MainActivity extends Activity {
    	private Context mContext;
        private ListView mListView;
        private ListViewAdapter mListViewAdapter;
        private ArrayList<String> mArrayList;
        private HashMap<String, String> mHashMap;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		init();
    	}
    
    	private void init(){
    		mContext=this;
    		mListView=(ListView) findViewById(R.id.listView);
    		mArrayList=new ArrayList<String>();
    		mArrayList.add("张三1");
    		mArrayList.add("李四1");
    		mArrayList.add("王五1");
    		mArrayList.add("张三2");
    		mArrayList.add("李四2");
    		mArrayList.add("王五2");
    		mArrayList.add("张三3");
    		mArrayList.add("李四3");
    		mArrayList.add("王五3");
    		mArrayList.add("张三4");
    		mArrayList.add("李四4");
    		mArrayList.add("王五4");
    		mListViewAdapter=new ListViewAdapter(mContext, mArrayList);
    		//过滤数据
    		//过滤出姓名里面包含"张"的数据
    		mListViewAdapter.getFilter().filter("张");
    		mListView.setAdapter(mListViewAdapter);
    	}
    
    }

    ListViewAdapter如下:

    package cc.testfilterable;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Filter;
    import android.widget.TextView;
    
    public class ListViewAdapter extends BaseAdapter {
    	private NameFilter mNameFilter;
    	private List<String> mArrayList;
    	private List<String> mFilteredArrayList;
    	private LayoutInflater mLayoutInflater;
    	public ListViewAdapter(Context context,List<String> arrayList) {
    		mArrayList = arrayList;
    		mLayoutInflater=LayoutInflater.from(context);
    		mFilteredArrayList=new ArrayList<String>();
    	}
    
    	@Override
    	public int getCount() {
    		if (mArrayList == null) {
    			return 0;
    		} else {
    			return (mArrayList.size());
    		}
    
    	}
    
    	@Override
    	public Object getItem(int position) {
    		if (mArrayList == null) {
    			return null;
    		} else {
    			return mArrayList.get(position);
    		}
    	}
    
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
    	
    	public Filter getFilter() {  
           if (mNameFilter == null) {    
        	   mNameFilter = new NameFilter();    
           }    
          return mNameFilter;  
        }  
    
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View itemView = null;
    		itemView = convertView;
    		ViewHolder viewHolder = null;
    		if (itemView == null) {
    			itemView = mLayoutInflater.inflate(R.layout.listviewitem, null);
    			viewHolder = new ViewHolder();
    			viewHolder.textView = (TextView) itemView.findViewById(R.id.textView);
    			itemView.setTag(viewHolder);
    		} else {
    			viewHolder = (ViewHolder) itemView.getTag();
    		}
    
    		if (mArrayList != null) {
    			if (viewHolder.textView != null) {
    				viewHolder.textView.setText((mArrayList.get(position)));
    			}
    
    		}
    
    		convertView = itemView;
    		return convertView;
    	}
    	
    
    	private class ViewHolder {
    		TextView textView;
    	}
    	
    	
    	//过滤数据
    	class NameFilter extends Filter {
    		//执行筛选
    		@Override
    		protected FilterResults performFiltering(CharSequence charSequence) {
    			FilterResults filterResults = new FilterResults();
    			for (Iterator<String> iterator = mArrayList.iterator(); iterator.hasNext();) {
    				String name = iterator.next();
    				System.out.println("---> name=" + name);
    				if (name.contains(charSequence)) {
    					mFilteredArrayList.add(name);
    				}
    			}
    			filterResults.values = mFilteredArrayList;
    			return filterResults;
    		}
    
    		//筛选结果
    		@Override
    		protected void publishResults(CharSequence arg0, FilterResults results) {
    			mArrayList = (List<String>) results.values;
    			if (results.count > 0) {
    				notifyDataSetChanged();
    			} else {
    				notifyDataSetInvalidated();
    			}
    
    		}}
    	
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
         />
    
    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >
        <TextView 
           android:id="@+id/textView"
           android:layout_width="wrap_content"
           android:layout_height="50dip"
           android:gravity="center"
        />    
       
    </LinearLayout>
  • 相关阅读:
    Nginx 相关介绍(Nginx是什么?能干嘛?)
    springMVC源码学习之:springMVC响应请求的几种方法
    springMvc源码学习之:spirngMvc获取请求参数的方法
    java编程之:org.apache.commons.lang3.text.StrTokenizer
    java的nio之:java的nio的服务器实现模型
    java定时框架:表达式设置
    java的nio之:浅析I/O模型
    java的nio之:unix内核的五种I/O通信模型
    java的nio之:java的bio流下实现的socket服务器同步阻塞模型和socket的伪异步的socket服务器的通信模型
    java.lang.OutOfMemoryError: unable to create new native thread如何解决
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3684986.html
Copyright © 2020-2023  润新知