Android Programming Tutorials

(Romina) #1
Getting More Active

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

helper=new RestaurantHelper(this);

name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.addr);
notes=(EditText)findViewById(R.id.notes);
types=(RadioGroup)findViewById(R.id.types);

model=helper.getAll();
startManagingCursor(model);
adapter=new RestaurantAdapter(model);
setListAdapter(adapter);
}

Step #5: 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.


115
Free download pdf