I have used SimpleCursorAdapter for display the data into a list in my activity class.I am getting the data from Sqlite DB and applying to listview.I am inserting the data from service class.In my service class i am getting the data from WEB services and inserting latest into Sqlite mobile database this service class will run on every 5 sec’s for check the latest at Web service then bring it and insert into Sqlite db.when ever the new record is inserted into Sqlite db the listview is not updating with latest inserted record.I have implemented my application as follows:
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;
}
}
}
From the above service class i am getting latest records and inserting to Sqlite db.
How can i update from this service class to View class?
please any body help me
You can create a broadcast using intent to call a function in the activity from the run() method of service
And in your activity you have to receive the broadcast intent and update the list