ViewPager PagerAdapter with cursor - CursorLoader.onLoadFinished doesnt get called with different query

前端 未结 1 996
心在旅途
心在旅途 2020-12-28 23:27

I am doing like a quote application that pulls the quotes from the database and I need to display quotes in a ViewPager.

I have created my CursorPagerAdapter which

相关标签:
1条回答
  • 2020-12-28 23:37

    MainActivity - add/modify:

    String getSelection(){
        if(null != mSearchKeyword && mSearchKeyword.getLength()>0) {
            return QuoteContentProvider.COLUMN_AUTHOR + " LIKE '%" + mSearchKeyword + "%' OR " +    
                QuoteContentProvider.COLUMN_MESSAGE + " LIKE '%" + mSearchKeyword + "%'";
        } else {
            return null;
        }       
    }
    
    String mSearchKeyword = null;
    
    public void search(String keyword) {
        mSearchKeyword = keyword;
        Log.d(MainActivity.TAG, "Search keyword: " + keyword);
        getLoaderManager().restartLoader(-1, null, this);
    }
    
    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        return new CursorLoader(this, QuoteContentProvider.CONTENT_URI, null, getSelection(), null, null);
    }
    

    Once the method search is fired, call getLoaderManager().restartLoader() on your Loader. Your old data will be discarded and restartLoader will trigger onCreateLoader to be called again.


    Do not forget close old cursor after it is swapped

    0 讨论(0)
提交回复
热议问题