views:

65

answers:

1

I'm watching the 2010 Google I/O video on this topic and I have a few questions to make sure I understand properly.

Google I/O 2010 - Android REST client applications

Please note I also have a very limited understanding of CursorAdapters

Right now my application just has a UI layer. I've created an object called DbAdapter using some Google tutorials to create a small database and a table in that database.

I've created a class I'm calling DataBroker (extending ContentProvider) to manage retrieving data from the database and calling web services to update data.

Scenarios:

1) Suppose I want to display a list of items in my ListActivity. Am I correct in assuming I will write a function in my DataBroker that creates a Cursor to the table in my database that holds those items, fire up a service which launches a thread which calls a web service to get additional items, then return the Cursor to my UI thread? Doing this would likely return the Cursor to my UI thread before the web service finished, but, I'm assuming, once the CursorAdapter has a Cursor, it will automatically show any changes made to that database table, yes?

2) Now I have a list of items in my ListView. Suppose I give the user some functionality to delete an item. The user executes this functionality. It should call a function in my DataBroker which updates the row in my database corresponding to that item, setting the status column to STATE_DELETING (which will remove the row from the ListView because the Cursor has a clause excluding this state), then fire off a service that fires a thread that hits my web service, then deletes the row in the database when finished?

Also, suppose I have multiple Activities, am I looking at having a static Cursor for each Activity with a list, grabbing that Cursor in the Activity's onCreate, and if that Cursor is null, hitting my DataBroker?

A: 

Since no one has responded, I'll respond with what I've done (and seems to be working)

It should be noted that I have renamed DataBroker. My ContentProvider is now called by some other name and I now have an extended Service which I have named DataBroker.

1) I do not do my Cursor query inside a Service. I call for the Cursor in the Activity and then call upon the Service (DataBroker) to query the web service for new data. If new data is found, the DataBroker broadcasts a message, received by any Activity listening, and the Activity then re-queries the Cursor.

2) I hand, via an Intent, the ID of the row needing deleted to my Service (DataBroker). The Service marks the status column for that ID as STATE_DELETING, uses a web service to let my server know the row has been deleted, and, if confirmed by the server, then deletes the row from my database. Also, after the row has been marked STATE_DELETING, I broadcast a message which may be subscribed to by any Activities interested letting them know I've done so. That way the Activity can re-query its Cursor (which does not return STATE_DELETING rows).

Andrew