2012-02-29 49 views
1

我已經使用SimpleCursorAdapter將數據顯示到我的活動類的列表中。我得到data from Sqlite DB and applying to listview。我從服務類中插入數據。在我的服務類中,我從WEB服務獲取數據並將最新插入到Sqlite移動數據庫這個服務類將每5秒運行一次以檢查最新的Web服務,然後將其插入Sqlite db.when將新記錄插入到Sqlite數據庫中後,listview不會隨着最新插入的記錄進行更新。我已經實現我的應用程序如下:我如何更新Service類的SimpleCursorAdapter列表視圖?

MyActivity.java

private MySqliteHelper msh; 
ListView lst; 
Cursor cursor; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     msh = new MySqliteHelper(MyActivity.this); 
     msh.openToWrite(); 
     lst = ((ListView)findViewById(R.id.listView1)); 
     cursor = msh.queueAll(); 

     String getFromDB[] = new String[]{MySqliteHelper.KEY_CONTENT1,MySqliteHelper.KEY_CONTENT2}; 
     int toView[] = new int[]{R.id.msgId,R.id.usrmsg}; 

     lst.setAdapter(new SimpleCursorAdapter(this, R.layout.list, cursor, getFromDB, toView)); 

     //calling service class 
     bindService(new Intent(ShoutGetMessagesActivity.this, ShoutRepeatService.class), mConnection, Context.BIND_AUTO_CREATE); 

     updateList(); 
    } 

     private void updateList(){ 
       cursor.requery(); 
       } 


     RepeatService bg; 
     private ServiceConnection mConnection = new ServiceConnection() { 

      public void onServiceConnected(ComponentName className, IBinder binder) { 

       bg = ((RepeatService.MyBinder) binder).getService(); 

       Toast.makeText(MyActivity.this, "Connected", 
         Toast.LENGTH_SHORT).show(); 

      } 

      public void onServiceDisconnected(ComponentName className) { 
       bg = null; 
      } 
     }; 
    } 

RepeatService.java

ArrayList<NewMessages> result; 
    private MySqliteHelper msh; 

    private Timer timer = new Timer(); 
    private static final long UPDATE_INTERVAL = 500; 



    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
      msh = new MySqliteHelper(this); 
      msh.openToWrite(); 

     pollForUpdates(); 

    super.onCreate(); 
    } 

     private void pollForUpdates() { 
     timer.scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID); 

       if(MySqliteHelper.LastMsgID==null){ 

        MySqliteHelper.LastMsgID="0"; 

       } 

       result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID)); 


       for(int i=0;i<result.size();i++){ 
        String msgs =result.get(i).getMessages(); 
        String msgID = result.get(i).getMsgId(); 
         Log.v("messages", "------------>>>>"+msgs); 
         Log.v("messageIDs", "--------->>>>>>"+msgID); 
         msh.insert(msgID, msgs); 
        } 

      } 
     }, 0, UPDATE_INTERVAL); 

     Log.v(getClass().getSimpleName(), "Timer started."); 

    } 



     private void updateList(){ 
     cursor.requery(); 
     } 

    public class MyBinder extends Binder { 
     RepeatService getService() 
     { 
      return RepeatService.this; 
     } 
    } 
} 

從上面的服務類我得到的最新記錄,並插入到sqlite的分貝。 如何從此服務類更新到視圖類?

請任何機構幫助我

回答

5

可以使用意圖從服務

private void pollForUpdates() { 
timer.scheduleAtFixedRate(new TimerTask() { 
    @Override 
    public void run() { 
     Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID); 

     if(MySqliteHelper.LastMsgID==null){ 

      MySqliteHelper.LastMsgID="0"; 

     } 

     result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID)); 


     for(int i=0;i<result.size();i++){ 
      String msgs =result.get(i).getMessages(); 
      String msgID = result.get(i).getMsgId(); 
       Log.v("messages", "------------>>>>"+msgs); 
       Log.v("messageIDs", "--------->>>>>>"+msgID); 
       msh.insert(msgID, msgs); 
      } 

    } 
************************************* 
Intent intent = new Intent(); 
intent.setAction("com.example.updatelist"); 
sendBroadcast(intent); // finally broadcast 
************************************* 

}, 0, UPDATE_INTERVAL); 

Log.v(getClass().getSimpleName(), "Timer started."); 

} 

的run()方法而在你的活動中調用活性的功能已接收廣播意圖創建一個廣播和更新列表

// Register Broadcast Receiver to receive broadcast intent in oncreate method 

public oncreate() 
{  
IntentFilter filter = new IntentFilter("com.example.updatelist"); 
     registerReceiver(myReceiver, filter); 
} 

private BroadcastReceiver myReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    msh = new MySqliteHelper(MyActivity.this); 
    msh.openToWrite(); 
    lst = ((ListView)findViewById(R.id.listView1)); 
    cursor = msh.queueAll(); 

    String getFromDB[] = new String[] MySqliteHelper.KEY_CONTENT1,MySqliteHelper.KEY_CONTENT2}; 
    int toView[] = new int[]{R.id.msgId,R.id.usrmsg}; 

    lst.setAdapter(new SimpleCursorAdapter(this, R.layout.list, cursor, getFromDB, toView)); 

    } 
}; 
+0

謝謝你的回答,但是最近的位置添加了lates記錄。有沒有機會在第一個位置添加lates記錄? – 2012-02-29 07:34:07

1

您可以在活動中使用的廣播接收器,並有服務發出每當更新數據廣播。而且你必須定義一個自定義廣播才能工作。

首先。 定義自定義操作這樣

public static final String DATA_INSERTED = "WHATEVER_YOU_WANT" 

當數據在服務發生變化,這樣做

Intent intent = new Intent (DATA_INSERTED); 
sendBroadcast(intent) 

初始化一個自定義廣播接收器來聽這個特定DATA_INSERTED行動。覆蓋onReceive方法。在onReceive方法中。您必須在適配器上調用notifydatasetchanged,這也意味着您需要對simplecursoradapter的引用。我會將廣播接收器的實施留給你弄清楚。

+0

你可以給我一些代碼解釋 – 2012-02-29 03:49:52

+0

我編輯了迴應。 – CChi 2012-02-29 04:00:11

相關問題