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);
}
With this code, we pour the individual pieces of a restaurant (e.g., its name) into a ContentValues and tell the SQLiteDatabase to insert it into the database. We call getWritableDatabase() to get at the SQLiteDatabase. Our helper will automatically open the database in write mode if it has not already been opened by the helper before.
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);
}
With this code, we pour the individual pieces of a restaurant (e.g., its name) into a ContentValues and tell the SQLiteDatabase to insert it into the database. We call getWritableDatabase() to get at the SQLiteDatabase. Our helper will automatically open the database in write mode if it has not already been opened by the helper before.
Finally, we need to actually call insert() at the appropriate time. Right now, our Save button adds a restaurant to our RestaurantAdapter – now, we need it to persist the restaurant to the database. So, modify the onSave
object in LunchList to look like this:
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";
break;
case R.id.take_out:
type="take_out";
break;
case R.id.delivery:
type="delivery";
break;
}
helper.insert(name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
};
We simply get the four pieces of data from their respective widgets and call insert().
object in LunchList to look like this:
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";
break;
case R.id.take_out:
type="take_out";
break;
case R.id.delivery:
type="delivery";
break;
}
helper.insert(name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
};
We simply get the four pieces of data from their respective widgets and call insert().
No comments:
Post a Comment