Android Programming Tutorials

(Romina) #1
The Restaurant Store

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.


Step #5: Save a Restaurant to the Database.......................................


We are going to be replacing our restaurant object model (and its


associated ArrayList) with the database and a Cursor representing the roster


of restaurants. This will involve adding some more logic to RestaurantHelper


to aid in this process, while also starting to use it from LunchList.


First, add an import statement for android.content.ContentValues to


RestaurantHelper.


Then, implement insert() on RestaurantHelper as follows:


public void insert(String name, String address,
String type, String notes) {
ContentValues cv=new ContentValues();

cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);

getWritableDatabase().insert("restaurants", "name", cv);
}

96
Free download pdf