Pass the Restaurant _ID
Now, let's step back a bit and think about what we are trying to achieve. We want to be able to use DetailForm for both adding new restaurants and editing an existing restaurant. DetailForm needs to be able to tell those two scenarios apart. Also, DetailForm needs to know which item is to be edited.
To achieve this, we will pass an "extra" in our Intent that launches DetailForm, containing the ID (_id column) of the restaurant to edit. We will use this if the DetailForm was launched by clicking on an existing restaurant. If DetailForm receives an Intent lacking our "extra", it will know to add a new restaurant.
First, we need to define a name for this "extra", so add the following data member to LunchList:
public final static String ID_EXTRA="apt.tutorial._ID";
We use the apt.tutorial namespace to ensure our "extra" name will not collide with any names perhaps used by the Android system.
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:
@Override
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".
To achieve this, we will pass an "extra" in our Intent that launches DetailForm, containing the ID (_id column) of the restaurant to edit. We will use this if the DetailForm was launched by clicking on an existing restaurant. If DetailForm receives an Intent lacking our "extra", it will know to add a new restaurant.
First, we need to define a name for this "extra", so add the following data member to LunchList:
public final static String ID_EXTRA="apt.tutorial._ID";
We use the apt.tutorial namespace to ensure our "extra" name will not collide with any names perhaps used by the Android system.
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:
@Override
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".
No comments:
Post a Comment