I have an android application that has a background service which polls a web service for new information and writes it into a sqlite database for my application. The Application also has a list activity that displays the data from this sqlite database.
Basically I want the list to auto update while looking at it if new data comes into the sqlite database from the service writing to it. I could just provide a refresh button of sorts but it would be nice if it would auto update. Is there an easy way to do that? Should the service just fire an event that the listactivity subscribes to and invalidate the adapter? Or is there a way for the adapter to be based off of a cursor or something and automatically update when the underlying data changes? Just trying to architecturally think through this and want the best and most maintainable (and quick to develop) method. I’m curious if I’ve missed a possibility that would make this much easier.
By the way, I’m using Mono for Android (company policy, not my choice).
You could implement a dynamically registered
BrodcastReceiver(in theonResumecallback), in yourActivitycontaining the list, that could listen for a custom broadcast. When thisBroadcastReceiverreceives the custom broadcast you would update the cursor, call notifyDataSetChanged, in other terms, referesh the list. YourServicewill insert the new values in the database when it receives them and when it finishes that task it could send a broadcast, your custom broadcast to announce interested observers that new data is available. This will trigger the receiver in theActivityif the activity is in the foreground.I don’t know
Monofor Android to give you some code but you should be able to implement something from the above idea.