8

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

我创建了我的 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 异步加载数据库

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

}

一切正常,它很好地加载了所有引号,但现在我需要执行搜索。所以我做了一个查询数据库的方法

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。

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

4

1 回答 1

4

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

触发方法搜索后,在您的加载器上调用 getLoaderManager().restartLoader()。您的旧数据将被丢弃,restartLoader 将触发 onCreateLoader 再次被调用。


交换后不要忘记关闭旧光标

于 2012-10-06T21:51:33.900 回答