annuaire recherche
android Manifest Manifest.permission Manifest.permission_group android.accessibilityservice AccessibilityService android.accounts Account android.app NotificationManager android.bluetooth BluetoothAdapter BluetoothClass BluetoothClass.Device BluetoothClass.Device.Major BluetoothClass.Service BluetoothDevice BluetoothServerSocket BluetoothSocket android.content SharedPreferences android.database.sqlite SQLiteCursorDriver SQLiteOpenHelper android.graphics Bitmap android.location LocationListener Geocoder GpsStatus GpsStatus.Listener GpsStatus.NmeaListener GpsSatellite android.media AudioFormat AsyncPlayer AudioRecord AudioRecord.OnRecordPositionUpdateListener ThumbnailUtils AudioManager android.net TrafficStats MailTo LocalSocket android.os AsyncTask AsyncTask.Status CountDownTimer Message MessageQueue HandlerThread android.text Html android.util JsonWriter android.view ContextMenu ContextMenu.ContextMenuInfo Display ViewManager View ViewStub ViewTreeObserver ViewParent WindowManager GestureDetector Gravity MenuInflater ScaleGestureDetector SoundEffectConstants android.view.inputmethod InputConnection InputMethod InputMethodSession BaseInputConnection InputMethodManager android.widget AbsListView AbsListView.LayoutParams AbsListView.OnScrollListener AbsListView.RecyclerListener AbsoluteLayout AbsoluteLayout.LayoutParams AbsSeekBar AbsSpinner AdapterView AdapterView.AdapterContextMenuInfo AdapterView.OnItemLongClickListener AdapterView.OnItemSelectedListener AdapterView.OnItemClickListener AnalogClock BaseAdapter BaseExpandableListAdapter Button CheckBox CheckedTextView Checkable Chronometer Chronometer.OnChronometerTickListener CompoundButton CompoundButton.OnCheckedChangeListener CursorAdapter CursorTreeAdapter DatePicker DatePicker.OnDateChangedListener DialerFilter DigitalClock EditText Filter Filter.FilterListener Filter.FilterResults ExpandableListAdapter ExpandableListView.OnChildClickListener ExpandableListView.OnGroupClickListener ExpandableListView.OnGroupCollapseListener ExpandableListView.OnGroupExpandListener Filterable Gallery Gallery.LayoutParams GridView GridLayout GridLayout.Alignment RadioGroup ImageView ImageView.ScaleType HorizontalScrollView ImageButton ImageSwitcher FilterQueryProvider ListAdapter ListView MediaController MultiAutoCompleteTextView MultiAutoCompleteTextView.CommaTokenizer MultiAutoCompleteTextView.Tokenizer QuickContactBadge RadioButton RatingBar RatingBar.OnRatingBarChangeListener RelativeLayout RemoteViews ResourceCursorAdapter ResourceCursorTreeAdapter Scroller ScrollView SearchView SearchView.OnCloseListener SearchView.OnQueryTextListener SearchView.OnSuggestionListener SeekBar SeekBar.OnSeekBarChangeListener SimpleAdapter SimpleAdapter.ViewBinder SimpleCursorAdapter SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.ViewBinder SimpleCursorTreeAdapter SimpleCursorTreeAdapter.ViewBinder SimpleExpandableListAdapter SlidingDrawer SlidingDrawer.OnDrawerCloseListener SlidingDrawer.OnDrawerOpenListener SlidingDrawer.OnDrawerScrollListener Spinner SpinnerAdapter WrapperListAdapter TabHost TabHost.TabSpec TextView TimePicker TimePicker.OnTimeChangedListener Toast TableLayout TableLayout.LayoutParams TableRow TableRow.LayoutParams TabWidget TextSwitcher ToggleButton TwoLineListItem VideoView ViewAnimator ViewFlipper ViewSwitcher ViewSwitcher.ViewFactory ZoomButtonsController ZoomButtonsController.OnZoomListener ZoomButton ZoomControls dalvik.system DexFile
personnages


Filter

翻译时间:2010-11-07

版本:Android 2.2 r1

 

结构

继承关系

public abstract class Filter extends Object

        

java.lang.Object

     android.widget.Filter

 

类概述

过滤器通过过滤模式来约束数据,通常由实现了Filterable接口的子类来生成。

过滤操作是通过调用 filter(CharSequence) 或者 filter(CharSequence, android.widget.Filter.FilterListener)这些异步方法来完成的。以上方法一旦被调用,过滤请求就会被递交到请求队列中等待处理,同时该操作会取消那些之前递交的但是还没有被处理的请求。

 

构造函数

         public Filter ()

创建一个新的异步过滤器。

 

公共方法

public CharSequence convertResultToString (Object resultValue)

    将受过滤的集合对象转换成CharSequence文本。所有继承了Filter的子类应该重写该方法。该方法的默认实现:如果参数为null则返回空字符串或者返回参数的字符串形式.

参数

                            resultValue  转换成CharSequence文本的对象

                   返回值

                            CharSequence 文本

                  

public final void filter(CharSequence constraint, Filter.FilterListener listener)

启动一个异步的过滤操作。对该方法的调用会取消之前队列中等待处理的过滤请求并且递交新的过滤请求等待执行。完成过滤操作之后,通知监听器。

参数

                            constraint  过滤数据的约束条件

listener  监听过滤操作完成之后发出的通知

参见

                            filter(CharSequence)

performFiltering(CharSequence)

publishResults(CharSequence, android.widget.Filter.FilterResults)

 

public final void filter(CharSequence constraint)

启动一个异步的过滤操作。对该方法的调用会取消之前队列中等待处理的过滤请求并且递交新的过滤请求等待执行。

参数

                            constraint  过滤数据的约束条件

                   参见

                            filter(CharSequence, android.widget.Filter.FilterListener)

 

受保护方法

protected abstract Filter.FilterResults performFiltering (CharSequence constraint)

根据约束条件调用一个工作线程过滤数据。子类必须实现该方法来执行过滤操作。过滤结果以Filter.FilterResults的形式返回,然后在UI线程中通过publishResults(CharSequence,android.widget.Filter.FilterResults)方法来发布。

约定:当约束条件为null时,原始数据必须被恢复。

参数

constraint       约束条件

返回值

过滤结果

参见

                       filter(CharSequence, android.widget.Filter.FilterListener)

                       publishResults(CharSequence, android.widget.Filter.FilterResults)

                        Filter.FilterResults

 

protected abstract void publishResults (CharSequence constraint, Filter.FilterResults results)

通过调用UI线程在用户界面发布过滤结果。子类必须实现该方法来显示performFiltering(CharSequence)的过滤结果。

参数

constraint       约束条件

results    过滤结果

参见

                       filter(CharSequence, android.widget.Filter.FilterListener)

performFiltering(CharSequence)

Filter.FilterResults

 

补充

示例代码

                   未过滤的数据如下图所示:

为过滤器设置约束条件(只显示年龄为22的用户):

         JavaTestFilter.java

public class TestFilter extends ListActivity {

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        ArrayList<HashMap<String,String>> list = new

                      ArrayList <HashMap<String, String>>();

        HashMap<String, String> map1 = new HashMap<String, String>();

        HashMap<String, String> map2 = new HashMap<String, String>();

        HashMap<String, String> map3 = new HashMap<String, String>();

        map1.put("name", "henly");

        map1.put("age", "22");

        map2.put("name", "john");

        map2.put("age", "23");

        map3.put("name", "lilei");

        map3.put("age", "22");

        list.add(map1);

        list.add(map2);

        list.add(map3);

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"name","age"}, new int[]{R.id.name,R.id.age});

        String str = new String("22");

        CharSequence constraint = str.subSequence(0, str.length());

        Filter filter = simpleAdapter.getFilter(); //得到一个过滤器

        filter.filter(constraint);  //为该过滤器设置约束条件

        setListAdapter(simpleAdapter);

    }

}

        

         XMLmain.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <LinearLayout android:id="@+id/listlinearlayout"

             android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:orientation="vertical">

             <ListView android:id="@id/android:list"

                       android:layout_width="fill_parent"

                       android:layout_height="wrap_content"

                       android:drawSelectorOnTop="false"

                       android:scrollbars="vertical" />

             </LinearLayout>

</LinearLayout>

 

XMLuser.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingLeft="12dip"

    android:paddingRight="12dip"

    android:paddingTop="1dip"

    android:paddingBottom="1dip"

    >

<TextView android:id="@+id/name"

         android:layout_width="150dip"

         android:layout_height="30dip"

         android:textSize="12pt"

         />

<TextView android:id="@+id/age"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textSize="12pt"

         />

</LinearLayout>


Article précédent: Article suivant: