如何自动重新查询与LoaderManager [英] How to automatically re-query with LoaderManager

查看:290
本文介绍了如何自动重新查询与LoaderManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,显示了一个SQLite数据库和数据变化数据不断,所以很明显,我想我应该用一个LoaderManager来显示数据。

做我读了一些关于使用的SQLite的LoaderManager,看到亚历克斯Lokwood的回答有关实施CursorLoader并使用它,我看到了有关直接使用CursorLoader到一个SQLite数据库,而不是一个ContentProvider的这个答案并开始使用commonsware的<一个HREF =htt​​ps://github.com/commonsguy/cwac-loaderex相对=nofollow> Loaderex 。

我创造了这个类显示在ListView中的数据:

 公共类LandingPageActivity扩展FragmentActivity实现LoaderCallbacks&LT;光标&GT; {
    ListView控件列表;
    SimpleCursorAdapter mAdapter;

    私人SQLiteCursorLoader装载机= NULL;
    私人语境mContext;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_friends_and_threads);
        名单=(ListView控件)findViewById(R.id.list);
        mContext = LandingPageActivity.this;

        mAdapter =新SimpleCursorAdapter(这一点,android.R.layout.simple_list_item_1,空,
                新的String [] {} Properties.Title.columnName,新的INT [] {} android.R.id.text1,0);

        list.setAdapter(mAdapter);
        getSupportLoaderManager()initLoader(0,空,这一点)。
    }

    @覆盖
    公共装载机&LT;光标&GT; onCreateLoader(INT为arg0,捆绑ARG1){
        上下文C = mContext;
        查询字符串=SELECT * FROM TABLE_NAME;
        SQLiteOpenHelper DB = DatabaseHelper.getDatabase();
        装载机=新SQLiteCursorLoader(C,数据库,查询,NULL);
        返回装载机;
    }

    @覆盖
    公共无效onLoadFinished(装载机&LT;光标&GT;为arg0,光标ARG1){
        mAdapter.changeCursor(ARG1);
    }

    @覆盖
    公共无效onLoaderReset(装载机&LT;光标&GT;为arg0){
        mAdapter.changeCursor(空);
    }
}
 

在数据库从一个'BroadcastListener'改变而活动仍在运行的问题开始。

基本上就是我要找的是实时的观察对数据库的修改,但似乎(也是从的 Android的文档,我需要调用 restartLoader 功能时,我想重新查询数据库)。

有没有一种方法可以自动在后台重新查询?

更新:

我最初的想法是创建一个函数,重新启动加载程序,是这样的:

 公共静态无效restartLoader(上下文的背景下){
    context.getSupportLoaderManager()restartLoader(0,空,LandingPageActivity.this)。
}
 

,但它似乎污物和这样做的不是最好的方法。

解决方案
  

在数据库从一个'BroadcastListener'改变而活动仍在运行......有没有一种方法可以自动在后台重新查询问题开始?

的BroadcastReceiver 使用插入()更新()更换()删除() SQLiteCursorLoader 修改数据库。然后,光标将自动重新加载。

I have an app that shows data from a SQLite DB and the data changes constantly, so obviously, I thought I should use a LoaderManager to show the data.

do I read a bit about using the LoaderManager with SQLite and saw Alex Lokwood's answer about implementing the CursorLoader and using it, and I saw the this answer about using a CursorLoader directly to an SQLite DB instead of a ContentProvider and started using commonsware's Loaderex.

I created this class for showing the data in a ListView:

public class LandingPageActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
    ListView list;
    SimpleCursorAdapter mAdapter;

    private SQLiteCursorLoader loader = null;
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_friends_and_threads);
        list = (ListView) findViewById(R.id.list);
        mContext = LandingPageActivity.this;

        mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
                new String[] { Properties.Title.columnName }, new int[] { android.R.id.text1 }, 0);

        list.setAdapter(mAdapter);
        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        Context c = mContext;
        String query = "SELECT * FROM TABLE_NAME";
        SQLiteOpenHelper db = DatabaseHelper.getDatabase();
        loader = new SQLiteCursorLoader(c, db, query, null);
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        mAdapter.changeCursor(arg1);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        mAdapter.changeCursor(null);
    }
}

the problem starts when the database is changed from a 'BroadcastListener' while the activity is still running.

basically what I'm looking for is real-time observing of the database changes but it seems (also from the Android Documentation that I need to call the restartLoader function when I want to re-query the db).

is there a way to automate re-query behind the scenes?

UPDATE:

My initial thought was to create a function that restarts the loader, something like this:

public static void restartLoader(Context context){
    context.getSupportLoaderManager().restartLoader(0, null, LandingPageActivity.this);
}

but it seems crud and not the best way of doing this.

解决方案

the problem starts when the database is changed from a 'BroadcastListener' while the activity is still running... is there a way to automate re-query behind the scenes?

Have the BroadcastReceiver use the insert(), update(), replace(), and delete() methods on SQLiteCursorLoader to modify the database. Then, the Cursor will be re-loaded automatically.

这篇关于如何自动重新查询与LoaderManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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