带光标的ViewPager PagerAdapter – CursorLoader.onLoadFinished不会被不同的查询调用

我正在做一个报价应用程序,从数据库中拉引号,我需要在ViewPager中显示引号。

我创build了我的CursorPagerAdapter,这似乎很好。

public class MyCursorPagerAdapter extends PagerAdapter { private Cursor cursor; private LayoutInflater inflater; public MyCursorPagerAdapter(Context context, Cursor cursor) { Log.d(MainActivity.TAG, "MyCursorPagerAdapter.onCreate()"); this.cursor = cursor; this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void swapCursor(Cursor cursor) { //if(cursor != null) { Log.d(MainActivity.TAG, "swapCursor()."); this.cursor = cursor; //notifyDataSetChanged(); //} } /** * Metóda destroyItem */ @Override public void destroyItem(View view, int position, Object object) { //cursor.moveToPosition(position); ((ViewPager) view).removeView((LinearLayout) object); } /** * Vráti počet prvkov */ @Override public int getCount() { if(cursor == null) { return 0; } else { //return cursor.getCount(); return 1000; } } @Override public Object instantiateItem(View view, int position) { position = position % cursor.getCount(); cursor.moveToPosition(position); LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.pager_item, null); TextView messageTextView = (TextView) layout.findViewById(R.id.message_textview); TextView authorTextView = (TextView) layout.findViewById(R.id.author_textview); messageTextView.setText(cursor.getString(cursor.getColumnIndex(QuoteDatabaseHelper.COLUMN_MESSAGE))); authorTextView.setText(cursor.getString(cursor.getColumnIndex(QuoteDatabaseHelper.COLUMN_AUTHOR))); ((ViewPager) view).addView(layout); return layout; } /** * Metóda isViewFromObject */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } 

}

我也使用CursorLoader来asynchronous加载数据库

 public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myCursorPagerAdapter = new MyCursorPagerAdapter(this, null); pager = (ViewPager) findViewById(R.id.viewPager); pager.setAdapter(myCursorPagerAdapter); getSupportLoaderManager().initLoader(-1, null, this); } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { return new CursorLoader(this, QuoteContentProvider.CONTENT_URI, null, null, null, null); } @Override public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) { Log.d(MainActivity.TAG, "onLoadFinished()"); myCursorPagerAdapter.swapCursor(cursor); this.cursor = cursor; pager.setCurrentItem((int) (myCursorPagerAdapter.getCount() / 2), false); pager.startAnimation(animation); } @Override public void onLoaderReset(Loader<Cursor> arg0) { myCursorPagerAdapter.swapCursor(null); } } 

用ContentProvider

 public class QuoteContentProvider extends ContentProvider { @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, "RANDOM()"); cursor.setNotificationUri(getContext().getContentResolver(), uri); Log.d(MainActivity.TAG, "QuoteContentProvider.query() - cursor.getCount(): " + cursor.getCount()); // getContext().getContentResolver().notifyChange(uri, null); return cursor; } } 

一切运作良好,它加载所有报价很好,但现在我需要执行search。 所以我做了一个查询数据库的方法

 public void search(String keyword) { Log.d(MainActivity.TAG, "Search keyword: " + keyword); getContentResolver().query(QuoteContentProvider.CONTENT_URI, null, QuoteContentProvider.COLUMN_AUTHOR + " LIKE '%" + keyword + "%' OR " + QuoteContentProvider.COLUMN_MESSAGE + " LIKE '%" + keyword + "%'", null, null); } 

查询是好的,我可以看到ContentProvider.query()被调用并打印出cursor.getCount(),

但是,即使cursor.setNotificationUri()被设置了,MainActivity中的onLoadFinished也不会被调用,而且我认为它的工作原理是它在应用程序启动时第一次工作。

我实际上试图插入一些数据与内容提供者和调用getContext()。getContentResolver()。notifyChange(uri,null); 它已经触发了OnLoadFinished,所以我不知道为什么这个不同的查询不会触发OnLoadFinished。

非常感谢谁会理解这一点。

Solutions Collecting From Web of "带光标的ViewPager PagerAdapter – CursorLoader.onLoadFinished不会被不同的查询调用"

MainActivity – 添加/修改:

 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); } 

一旦方法search被触发,在Loader上调用getLoaderManager()。restartLoader()。 您的旧数据将被丢弃,并且restartLoader将触发onCreateLoader以再次调用。

交换后不要忘记closures旧光标