我已經使用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的分貝。 如何從此服務類更新到視圖類?
請任何機構幫助我
謝謝你的回答,但是最近的位置添加了lates記錄。有沒有機會在第一個位置添加lates記錄? – 2012-02-29 07:34:07