如果数据通过服务更改数据库,如何重新运行加载程序

时间:2012-11-28 08:57:07

标签: android android-fragments

如果在数据库中从服务更新数据时,如何重新运行Loader以更新适配器数据?

DataPullService:

public class DataPullService extends Service {


    private Timer nstimer=new Timer();

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NetworkConnection network=new NetworkConnection(this.getApplicationContext());
        boolean online = network.isOnline();
        if(online){
            try {
                nstimer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        DBHelper dbHelper=new DBHelper(getApplicationContext());
                        SQLiteDatabase mydb = dbHelper.getWritableDatabase();

                        ContentValues values=new ContentValues();
                        values.put(DBConstants.COMPANY, "Innodea");

                        mydb.insert(DBConstants.FAVORITE_TABLE, null, values);

                    }
                }, 0, 60000);
            } catch (Exception e) {
          }
        }
        return startId;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        nstimer.cancel();
    }
}

MyListFragment:

public class MyListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {


    private SimpleCursorAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText("List loading...");
        setHasOptionsMenu(true);

        mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, new String[]{DBConstants.SNO,DBConstants.COMPANY}, new int[]{android.R.id.text1,android.R.id.text2});

        setListAdapter(mAdapter);

        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        DBHelper dbHelper=new DBHelper(getActivity());
        return new LiveMyLoader(getActivity(),dbHelper);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
        mAdapter.swapCursor(c);
        setListAdapter(mAdapter);
    }

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


}

上面的代码在创建片段时加载数据。那么每当数据在DB中更改(由DataPullService更新)时如何更新此列表片段?

3 个答案:

答案 0 :(得分:1)

我在MyListFragment中添加了一个BroadcastReceiver,并在onReceive方法中重新启动了Loader。

在MyListFragment-onActivityCreated中:

mycontext=this;
getActivity().registerReceiver(FragmentReceiver1, new IntentFilter("fragmentupdater"));

在MyListFragment中

private final BroadcastReceiver FragmentReceiver1 = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            getLoaderManager().restartLoader(0, null, mycontext);
            Log.i("timer", "Broadcast received");
        }};

在MyListFragment-onDestroyView:

@Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unregisterReceiver(FragmentReceiver1);


}

在DataPullService中:

Intent intent=new Intent("fragmentupdater");
sendBroadcast(intent);

现在每当服务更新数据库中的数据时,广播接收器都会收到并重新启动加载程序。

注意:在Android清单文件中注册接收器。

答案 1 :(得分:0)

如果要刷新数据,请在Loader上调用getLoaderManager().restartLoader()restartLoader将触发onCreateLoader再次被调用。在这种情况下,它将加载新数据。

答案 2 :(得分:0)

1在类中创建静态列表。 2加载数据列表。 3每次更改数据库时重新加载列表(观察者模式) 4每次重新加载列表时调用notifyDatasetChanged()(在观察者的update()中) 注意:使用无限阵列适配器