Android Programming Tutorials

(Romina) #1
Getting More Active

return(true);
}

return(super.onOptionsItemSelected(item));
}

Here, we launch the DetailForm activity without our "extra", signalling to


DetailForm that it is to add a new restaurant. You will need imports again for


android.view.Menu, android.view.MenuInflater, and android.view.MenuItem.


Step #8: Detail Form Supports Add and Edit....................................


Last, but certainly not least, we need to have DetailForm properly do useful


work when the Save button is clicked. Specifically, we need to either insert


or update the database. It would also be nice if we dismissed the DetailForm


at that point and returned to the main LunchList activity.


To accomplish this, we first need to add an update() method to


RestaurantHelper that can perform a database update:


public void update(String id, String name, String address,
String type, String notes) {
ContentValues cv=new ContentValues();
String[] args={id};

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

getWritableDatabase().update("restaurants", cv, "_ID=?",
args);
}

Then, we need to adjust our onSave listener object in DetailForm to call the


right method (save() or update()) and finish() our activity:


private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;

switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:

119
Free download pdf