Wednesday, 30 January 2013

Get Access to the Helper | Android Tutorial pdf

Get Access to the Helper

We will be using RestaurantHelper as our bridge to the database. Hence, LunchList will need a RestaurantHelper, to retrieve existing restaurants and add new ones.
In order to really use the database, though, we need to open and close access to it from LunchList.
First, create a RestaurantHelper data member named helper.
Then, in onCreate() in LunchList, after the call to setContentView(), initialize RestaurantHelper like this:
helper=new RestaurantHelper(this);
Finally, implement onDestroy() on LunchList as follows:
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}

All we do in onDestroy(), besides chain to the superclass, is close the helper we opened in onCreate(). This will close the underlying SQLite database as well.

No comments: