views:

61

answers:

1

I have an Activity with a list that is bound to a ListAdapter reading data into a ArrayList from a database. All is well when the data is first loaded. While the Activity is open and the list is being displayed it is possible and likely that the data in the database will be updated by a service but the list does not reflect the changes because the ArrayList does not know about the changes. If the Activity is no longer in the foreground as would be the case if the user goes to the home screen and then is brought back to the foreground I would like for the Activity to not display what it did prior but rather reload the data using the ListAdapter the view is bound to. I think something needs to call finish() but I am not sure what. This is what I have in the Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
app = (MyApplication) getApplication();
adapter = new MyListAdapter(this, app.getMyEvents());
setListAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
adapter.forceReload();
}
+1  A: 

I have an Activity with a list that is bound to a ListAdapter reading data into a ArrayList from a database.

Why not use a CursorAdapter instead of reading everything into an ArrayAdapter?

While the Activity is open and the list is being displayed it is possible and likely that the data in the database will be updated by a service but the list does not reflect the changes because the ArrayList does not know about the changes.

If you used a CursorAdapter, you could just call requery() on the Cursor, and your list would update with the latest contents of the database. You could do that in onStart(), for example, and it would be triggered whenever the user comes back to the activity -- from the home screen, from an incoming phone call they took, etc.

CommonsWare
CursorAdaptor was the way to go. thanks