Android Programming Tutorials

(Romina) #1
Getting More Active

Next, convert the onListClick object to an onListItemClick() method


(available to us on ListActivity) and have it add this "extra" to the Intent it


uses to start the DetailForm:


public void onListItemClick(ListView list, View view,
int position, long id) {
Intent i=new Intent(LunchList.this, DetailForm.class);

i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}

The _id of the restaurant happens to be provided to us as the fourth


parameter to onListItemClick(). We turn it into a String because DetailForm


will want it in String format, as we will see shortly.


Next, add the following data member to DetailForm:


String restaurantId=null;

This will be null if we are adding a new restaurant or the string form of the


ID if we are editing an existing restaurant.


Finally, add the following line to the end of onCreate() in DetailForm:


restaurantId=getIntent().getStringExtra(LunchList.ID_EXTRA);

This will pull out our "extra", or leave restaurantId as null if there is no


such "extra".


Step #6: Load the Restaurant Into the Form.....................................


In the case where we are editing an existing restaurant, we need to load that


restaurant from the database, then load it into the DetailForm.


Since we created a RestaurantHelper in onCreate(), we need to close it again,


so add an onDestroy() implementation to DetailForm as follows:


116
Free download pdf