Android Programming Tutorials

(Romina) #1
The Restaurant Store

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().


Step #6: Get the List of Restaurants from the Database...................


Database


This puts restaurants into the database. Presumably, it would be useful to


get them back out sometime. Hence, we need some logic that can query the


database and return a Cursor with columnar data from our restaurant table.


A Cursor in Android is much like a cursor in other database access libraries


97
Free download pdf