如何更新通过SimpleCursorAdapter从数据库查询数据的ListView?

发布于 2024-12-26 17:26:47 字数 2116 浏览 0 评论 0原文

我想使用 SimpleCursorAdapter 在列表视图中显示从数据库查询的项目。 例如,数据库中可能有 20,000 个项目。我想只加载100个查询的项目(_id:1-100)而不是加载所有项目,当滚动到listview的末尾时,加载另外100个查询的项目(_id:101-200),如何实现?欢迎任何建议,谢谢。

相关代码如下:

protected void onCreate(Bundle savedInstanceState) {
    mCursor = managedQuery(CONTENT_URI, PROJECTION, null, null, "_id DESC");
    mAdapter = new SimpleCursorAdapter(this,R.layout.list_content,  mCursor, keys,  values);
    setListAdapter(mAdapter);
}

在我定义的列表视图中,我想通过查询数据库加载更多项目。

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
{
    int lastItem = firstVisibleItem + visibleItemCount - 1;

    if (mListAdapter != null) {
        if ((lastItem == mListAdapter.getCount()-1) && (mRefreshState != REFRESHING)) {
            mFooterView.setVisibility(View.VISIBLE);
            mRefreshState = REFRESHING;
            new Handler().postDelayed(new Runnable() {  
                public void run() {  
                    //execute the task ,  i want to load more items by query database
                    RefreshListView(LOADING_STORED_INFO);                                           
                }  
             }, DEFAULT_DELAY_TIMER);           
        }
    }
}

在AsyncTask加载数据中,我做了查询操作。

    protected Integer doInBackground(Integer... params)
    { 
        Uri uri = ContentUris.withAppendedId(CONTENT_URI, mCursor.getInt(0)-1);
        cursor = managedQuery(uri, PROJECTION, null, null, "_id DESC");
        return (0 == params[0]) ? 1 : 0;
    }

    @Override
    protected void onPostExecute(Integer result)
    {
        mAdapter.changeCursor(cursor);//is this OK?
        mAdapter.notifyDataSetChanged();
        /*
        if (1 == result)
        {
            mListView.setSelection(1);
        }
        else
        {
            mListView.setSelection(mCount-1);               
        }*/
        // Call onRefreshComplete when the list has been refreshed.
        mListView.onRefreshComplete(result); 
        super.onPostExecute(result);
    } 

I want to show the items queried from database in the listview with SimpleCursorAdapter.
For example, there may be 20,000 items in the database. I want to just load 100 items(_id : 1-100) queried instead of load all items, when scrolling in the end of listview, load another 100 items(_id : 101-200) queried, how to achieve it? Any suggestion is welcome, thanks.

Relative codes are as follows:

protected void onCreate(Bundle savedInstanceState) {
    mCursor = managedQuery(CONTENT_URI, PROJECTION, null, null, "_id DESC");
    mAdapter = new SimpleCursorAdapter(this,R.layout.list_content,  mCursor, keys,  values);
    setListAdapter(mAdapter);
}

In my defined listview, i want to load more items by query database.

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
{
    int lastItem = firstVisibleItem + visibleItemCount - 1;

    if (mListAdapter != null) {
        if ((lastItem == mListAdapter.getCount()-1) && (mRefreshState != REFRESHING)) {
            mFooterView.setVisibility(View.VISIBLE);
            mRefreshState = REFRESHING;
            new Handler().postDelayed(new Runnable() {  
                public void run() {  
                    //execute the task ,  i want to load more items by query database
                    RefreshListView(LOADING_STORED_INFO);                                           
                }  
             }, DEFAULT_DELAY_TIMER);           
        }
    }
}

In the AsyncTask loading data, i do the query operation.

    protected Integer doInBackground(Integer... params)
    { 
        Uri uri = ContentUris.withAppendedId(CONTENT_URI, mCursor.getInt(0)-1);
        cursor = managedQuery(uri, PROJECTION, null, null, "_id DESC");
        return (0 == params[0]) ? 1 : 0;
    }

    @Override
    protected void onPostExecute(Integer result)
    {
        mAdapter.changeCursor(cursor);//is this OK?
        mAdapter.notifyDataSetChanged();
        /*
        if (1 == result)
        {
            mListView.setSelection(1);
        }
        else
        {
            mListView.setSelection(mCount-1);               
        }*/
        // Call onRefreshComplete when the list has been refreshed.
        mListView.onRefreshComplete(result); 
        super.onPostExecute(result);
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-01-02 17:26:48

以这种方式在 SQL 查询中使用 LIMIT 语句:

SELECT your_column FROM your_table ORDER BY your_order LIMIT limit_skip, limit_count

然后您可以使用 OnScrollListener 检索第一个可见单元格的索引和可见单元格的数量,以便您可以增加 limit_skiplimit_count 连贯。

使用 CursorLoader< 代替通用的 AsyncTask /code>并实现 LoaderManager.LoaderCallbacks 如下:

public Loader<Cursor> onCreateLoader(int id, Bundle args){
    String orderBy = "_id DESC"
    if(args != null){
        orderBy += " LIMIT " + args.getInt("LIMIT_SKIP") + "," + args.getInt("LIMIT_COUNT");
    }

    return new CursorLoader(this /*context*/, CONTENT_URI, PROJECTION, null, null, orderBy);
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data){
    listAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader){
    listAdapter.swapCursor(null);
}

然后,在 onCreate() 中,将 null 传递为cursornew SimpleCursorAdapter() 并以这种方式创建 CursorLoader

getLoaderManager().initLoader(0, null, this /*LoaderCallbacks<Cursor>*/);

然后,在 onScroll() 中,重置每次加载器都以这种方式:

Bundle args = new Bundle();
args.putInt("LIMIT_SKIP", limit_skip_value);
args.putInt("LIMIT_COUNT", limit_count_value);
getLoaderManager().restartLoader(0, args, this /*LoaderCallbacks<Cursor>*/);

Use the LIMIT statement in the SQL query in this way:

SELECT your_column FROM your_table ORDER BY your_order LIMIT limit_skip, limit_count

Then you can use a OnScrollListener to retrieve the index of the first visible cell and the number of visible cells so you can increment limit_skip and limit_count coherently.

Instead of the generic AsyncTask use a CursorLoader and implement LoaderManager.LoaderCallbacks<Cursor> as follow:

public Loader<Cursor> onCreateLoader(int id, Bundle args){
    String orderBy = "_id DESC"
    if(args != null){
        orderBy += " LIMIT " + args.getInt("LIMIT_SKIP") + "," + args.getInt("LIMIT_COUNT");
    }

    return new CursorLoader(this /*context*/, CONTENT_URI, PROJECTION, null, null, orderBy);
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data){
    listAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader){
    listAdapter.swapCursor(null);
}

Then, in onCreate(), pass null as cursor to new SimpleCursorAdapter() and create the CursorLoader in this way:

getLoaderManager().initLoader(0, null, this /*LoaderCallbacks<Cursor>*/);

Then, in onScroll(), reset everytime the loader in this way:

Bundle args = new Bundle();
args.putInt("LIMIT_SKIP", limit_skip_value);
args.putInt("LIMIT_COUNT", limit_count_value);
getLoaderManager().restartLoader(0, args, this /*LoaderCallbacks<Cursor>*/);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文