2012-11-28 74 views
0

如何在從服務數據庫更新數據時重新運行加載程序以更新適配器數據?如何在服務器更改數據庫時重新運行加載程序

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


} 

上述代碼負載數據時片段created.Then如何更新此列表片段每當在DB改變的數據(由更新DataPullService)?

回答

0

當您想刷新數據時,請在您的Loader上撥打getLoaderManager().restartLoader()restartLoader將觸發onCreateLoader再次被調用。在這種情況下,它將加載新的數據。

+1

是的,如何從Service.getLoaderManager中調用getLoaderManager僅在Activity和fragment中可用 – Ramprasad

+1

是的,如何從服務重啓Loader – Ramprasad

0

1在類中創建靜態列表。 2加載數據列表。 3重載,只要更改數據庫中的表(Observer模式) 4調用notifyDatasetChanged()(您的觀察者的更新())重新加載列表中的每個時間 注:用無盡的陣列適配器

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

現在,只要在DB服務更新數據時,broadscast接收器接收並重新啓動加載器。

注意:在Android清單文件中註冊接收器。

+0

該方法是否正確?我讀了一些頁面,說不使用廣播接收器中的片段上下文? – Ramprasad

+0

有時我收到ANR錯誤信息,而廣播接收機試圖接收... – Ramprasad

+0

我喜歡這個,如果它的工作,因爲這意味着我不需要做組件之間的某種硬連接。 –

相關問題