ViewPager PagerAdapter光标 - CursorLoader.onLoadFinished犯规被调用不同的查询 [英] ViewPager PagerAdapter with cursor - CursorLoader.onLoadFinished doesnt get called with different query

查看:180
本文介绍了ViewPager PagerAdapter光标 - CursorLoader.onLoadFinished犯规被调用不同的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做这样的拉行情从数据库中引用的应用程序,我需要在一个ViewPager显示引号。

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

我创建了我CursorPagerAdapter这似乎运作良好。

I have created my CursorPagerAdapter which seems to work well.

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到异步加载数据库

I am also using CursorLoader to async-load the database

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

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

}

一切都工作得很好,它加载所有的报价很好,但现在我需要执行搜索。所以我作出这样的查询数据库的方法

Everything works well, it loads all quotes nicely, but now I need to perform search. So I make a method that queries the database

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()

The query is good, I can see ContentProvider.query() getting called and prints out cursor.getCount(),

但是现在onLoadFinished在MainActivity犯规,即使cursor.setNotificationUri()设置被调用,而我presume它的工作,因为它的作品第一次应用程序启动时。

其实,我试图插入一些数据与内容提供者和调用的getContext()getContentResolver()有NotifyChange(URI,空)。它已引发了OnLoadFinished,所以我不知道为什么这不同的查询犯规火OnLoadFinished。

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

Thank you very much who will make sense of this.

推荐答案

MainActivity - 添加/修改:

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

一旦方法搜索被激发,调用getLoaderManager()。restartLoader()在你的装载机。您的旧数据​​将被丢弃,restartLoader将触发onCreateLoader再次调用。

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

这篇关于ViewPager PagerAdapter光标 - CursorLoader.onLoadFinished犯规被调用不同的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆