CursorAdapter中的filter

还是从原来163博客中粘贴过来的。

CursorAdapter中的filter  

2009-11-25 18:52:47|  分类: Android学习随记 |  标签: |字号大中小 订阅

在看Contacts的代码的时候,发现在ContactsListActivity界面,按下a-z,0-9的时候,都会进行相应的匹配,将不符合匹配的联系人项隐藏起来.如图:

CursorAdapter中的filter - 光年 - 光年的个人主页

刚开始我以为是响应onKey()或者onKeyDown()什么的,找了半天。可是后来打印日志的时候发现调用了一个runQueryOnBackgroundThread()函数,在CursorAdapter中有它,使用了FilterQueryProvider这个东东~~

分享一下,免得走弯路了

 

CursorAdapter

 

    /**

     * Runs a query with the specified constraint. This query is requested

     * by the filter attached to this adapter.

     *

     * The query is provided by a

     * {@link android.widget.FilterQueryProvider}.

     * If no provider is specified, the current cursor is not filtered and returned.

     *

     * After this method returns the resulting cursor is passed to {@link #changeCursor(Cursor)}

     * and the previous cursor is closed.

     *

     * This method is always executed on a background thread, not on the

     * application's main thread (or UI thread.)

     *

     * Contract: when constraint is null or empty, the original results,

     * prior to any filtering, must be returned.

     *

     * @param constraint the constraint with which the query must be filtered

     *

     * @return a Cursor representing the results of the new query

     *

     * @see #getFilter()

     * @see #getFilterQueryProvider()

     * @see #setFilterQueryProvider(android.widget.FilterQueryProvider)

     */

    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {

        if (mFilterQueryProvider != null) {

            return mFilterQueryProvider.runQuery(constraint);// mFilterQueryProvider这个东西是FilterQueryProviderFilterQueryProvider的一个实例。

        }

        return mCursor;

    }

FilterQueryProvider

/**

 * This class can be used by external clients of CursorAdapter and

 * CursorTreeAdapter to define how the content of the adapter should be

 * filtered.

 *

 * @see #runQuery(CharSequence)

 */

public interface FilterQueryProvider {

    /**

     * Runs a query with the specified constraint. This query is requested

     * by the filter attached to this adapter.

     *

     * Contract: when constraint is null or empty, the original results,

     * prior to any filtering, must be returned.

     *

     * @param constraint the constraint with which the query must

     *        be filtered

     *

     * @return a Cursor representing the results of the new query

     */

    Cursor runQuery(CharSequence constraint);

}

在ContactsListActivity中,原理是一样的。代码如下

 

ContactsListActivity

        /**

         * Run the query on a helper thread. Beware that this code does not run

         * on the main UI thread!

         */

        @Override

        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {

            return doFilter(constraint.toString());//doFilter()貌似实现了FilterQueryProvider的功能

        }

 

 

doFilter

    /**

     * Called from a background thread to do the filter and return the resulting cursor.

     *

     * @param filter the text that was entered to filter on

     * @return a cursor with the results of the filter

     */

    Cursor doFilter(String filter) {

        final ContentResolver resolver = getContentResolver();

 

        switch (mMode) {

           ……

                ……

              uri = Uri.withAppendedPath(mGroupFilterUri, Uri.encode(filter));

              return resolver.query(uri,……);

        }

        throw new UnsupportedOperationException("filtering not allowed in mode " + mMode);

    }

 

是的,你可以使用 SQLite 数据库来存储用户输入的课程信息。以下是一个简单的 Java 代码示例,它使用 SQLiteOpenHelper 类来创建和管理数据库: ```java public class CourseDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "course.db"; private static final int DATABASE_VERSION = 1; public CourseDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE courses (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "course_name TEXT, course_day_of_week INTEGER, " + "course_start_time INTEGER, course_end_time INTEGER);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Implement database upgrade logic here } } ``` 在这个示例,我们创建了一个名为“courses”的表,其包括课程名称、星期几、开始时间和结束时间等字段。 接着,我们可以使用 CursorAdapter 来将数据库的数据显示在课程表。以下是一个简单的代码示例,它使用 SimpleCursorAdapter 类来实现: ```java public class CourseListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { private static final int LOADER_ID = 0; private CourseDatabaseHelper mDbHelper; private SimpleCursorAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDbHelper = new CourseDatabaseHelper(getActivity()); mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item_course, null, new String[] { "course_name", "course_day_of_week", "course_start_time", "course_end_time" }, new int[] { R.id.course_name, R.id.course_day_of_week, R.id.course_start_time, R.id.course_end_time }, 0); setListAdapter(mAdapter); getLoaderManager().initLoader(LOADER_ID, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(getActivity(), CourseProvider.CONTENT_URI, null, null, null, null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } } ``` 在这个示例,我们创建了一个名为“CourseListFragment”的 Fragment,它使用 SimpleCursorAdapter 来将数据库的数据显示在 ListView 。我们使用 LoaderManager 来异步加载数据,并在数据加载完成后更新 ListView。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值