同步服务运行后刷新片段FragmentActivity [英] Refreshing fragments in FragmentActivity after sync service runs

本文介绍了同步服务运行后刷新片段FragmentActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有任何人有一个 FragmentActivity的刷新查看片段任何优雅的解决方案 ViewPager 同步后服务 SyncAdapter 运行?

does anybody have any elegant solution for refreshing the Views in Fragments in a FragmentActivity's ViewPager after a sync Service from a SyncAdapter runs?

我已经打过电话 notifyDataSetChanged() notifyDataSetInvalidated()我的适配器,以及 refreshDrawableState()我的意见( GridView的),但无济于事。也许我已经从错误的地方叫他们 - 我试过在 setUserVisibleHint ,其中 ISVISIBLE =做真的,希望触发它每当片段映入眼帘,但它不能正常工作。

I've tried calling notifyDataSetChanged() and notifyDataSetInvalidated() on my adapter, as well as refreshDrawableState() on my views (GridViews), but to no avail. Perhaps I've been calling them from the wrong places -- I've tried doing it at setUserVisibleHint where isVisible=true, hoping to trigger it whenever the fragment comes into view, but it doesn't work.

我也一直在使用异步调用的SQLite数据库为我的数据的需求,而不是内容提供商,我认为会作出这个更容易一些。我能想到的几个方法可以做到这一点没有内容提供商,但也不是很不错的。

I've also been using ASync calls to the SQLite database for my data needs, rather than a Content Provider, which I think would have made this a bit easier. I can think of a couple of ways to do it without a Content Provider, but neither are very nice.

任何想法?我可以提供code。如果希望。谢谢你。

Any ideas? I can provide code if wished. Thanks.

推荐答案

我假设你正在使用的AsyncTask加载光标只是为了解释的缘故,但它会工作,如果你是相同的使用装载机,一个线程池或什么的。

I'll assume that you're using an AsyncTask for loading the cursor just for the sake of the explanation, but it would work the same if you're using a Loader, an ThreadPool or whatever.

从服务,一旦新数据被改变,我要送一个 LocalBroadcast 。该活动可能是有或没有,所以广播是让它知道有新的数据的好办法。因此,从服务,你会怎么做:

From the service, as soon as new data was changed I would send a LocalBroadcast. The activity might be there or not, so a broadcast is a good way to let it know there's new data. So from the service you would do:

    // that's an example, let's say your SyncAdapter updated the album with this ID
    // but you could create a simply "mybroadcast", up to you.
    Intent i = new Intent("albumId_" + albumId);
    LocalBroadcastManager.getInstance(this).sendBroadcast(i);

,然后从活动/片段具有光标,你会听这个广播是这样的:

and then from the activity/fragment that have the Cursor, you'll be listening to this broadcast like this:

    public void onResume(){
        // the filter matches the broadcast
        IntentFilter filter = new IntentFilter("albumId_" + albumId); 
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
    }
    public void onPause(){
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
    }

    // and of course you have to create a BroadcastReceiver
    private BroadcastReceiver myReceiver = new BroadcastReceiver(){
       @Override
       public void onReceive(Context context, Intent intent){
           // here you know that your data have changed, so it's time to reload it
           reloadData = new ReloadData().execute(); // you should cancel this task onPause()
       }
    };

就像我说的,这下一部分取决于你使用加载光标穿什么方法,在这个例子中,我会在AsyncTask的表现,因为它是非常受欢迎的(但我真的相信你,并在每一个开发人员世界应该使用装载机模式)。

as I said, this next part varies depending on what threading method you're using to load the Cursor, for this example I'll show in a AsyncTask because it's very popular (but I really believe you and every developer in the world should use the Loaders pattern).

private class ReloadData extends AsyncTask<Void, Void, Cursor> {
 protected Cursor doInBackground(Void... void) {
     // here you query your data base and return the new cursor
     ... query ...
     return cursor;
 }

 protected void onPostExecute(Cursor result) {
     // you said you're using a subclass of CursorAdater
     // so you have the method changeCursor, that changes the cursor and closes the old one
     myAdapter.changeCursor(result);
 }

}

以上的方法,我测试和使用过,我知​​道它的工作原理。有使其与标志 FLAG_REGISTER_CONTENT_OBSERVER 和覆盖 onContentChanged()重新执行查询和交换工作的一种方式光标,但我从来没有测试过。这将是类似的东西:

The above approach I tested and used before and I know it works. There's a way of making it work with the flag FLAG_REGISTER_CONTENT_OBSERVER and override onContentChanged() to re-execute the query and swap the cursor, but I've never tested it. It will be something like that:

初​​始化适配器的构造的CursorAdapter(上下文的背景下,光标C,INT标志)传递标志 FLAG_REGISTER_CONTENT_OBSERVER 和覆盖 onContentChanged()。内部onContentChanged您将执行的AsyncTask就像上面。这样,您就不必使用 LocalBroadcastManager 作为数据库将提醒。之所以说方法不是我的主要的回答,那是因为我从来没有测试过。

init your adapter with the constructor CursorAdapter(Context context, Cursor c, int flags) passing the flag FLAG_REGISTER_CONTENT_OBSERVER and override onContentChanged(). Inside onContentChanged you will execute the AsyncTask just like above. This way you don't have to use the LocalBroadcastManager as the database will alert. The reason that method is not my main answer, it's because I've never tested it.

注意 autoRequery 已去precated和它的鼓励,因为它在UI线程中执行数据加载。

Note that autoRequery have been deprecated and it's discouraged as it performs data loading in the UI thread.

编辑:

我只注意到其内容的观察者是一个API 11件事。你有两个选择:1使用支持库,而不是:的https://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html或广播选项。

I just noticed that the content observer is an API 11 thing. You have two options: 1 use the support library instead: https://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html or the broadcast option.

这篇关于同步服务运行后刷新片段FragmentActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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