Android设置SimpleAdapter的Filter过滤数据

MainActivity如下:

package ccc.testfilterofadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.app.Activity;
/**
 * Demo描述:
 * 设置SimpleAdapter的Filter
 * 这样的局限性很大.
 * 对条件做复杂限制的应该定义Adapter且实现Filterable接口
 * 
 * 参考资料:
 * 1 http://winuxxan.blog.51cto.com/2779763/509931
 * 2 http://www.cnblogs.com/over140/archive/2010/11/25/1887892.html
 *   Thank you very much
 */
public class MainActivity extends Activity {
    private ListView mListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init(){
		mListView=(ListView) findViewById(R.id.listView);
        List<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
      	HashMap<String, Object> hashMap=null;
      	for (int i = 0; i < 5; i++) {
      		hashMap=new HashMap<String, Object>();
      		hashMap.put("name", "小明"+i);
          	hashMap.put("phone", "135"+i);
          	hashMap.put("amount","500"+i);
        	list.add(hashMap);
		}
      	for (int i = 0; i < 5; i++) {
      		hashMap=new HashMap<String, Object>();
      		hashMap.put("name", "小张"+i);
          	hashMap.put("phone", "138"+i);
          	hashMap.put("amount","700"+i);
        	list.add(hashMap);
		}
      	
      SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.listviewitem, 
      new String []{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});
      //为Adapter设置过滤条件
      adapter.getFilter().filter("小张");
      mListView.setAdapter(adapter);
	}
}


main.xml如下:

<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"
        android:layout_centerInParent="true" />

</RelativeLayout>

listviewitem如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="100dip"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/phone"
        android:layout_width="150dip"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Android设置SimpleAdapter的Filter过滤数据

上一篇:判断手机端和pc端


下一篇:判断手机机型和浏览器内核