Android Programming Tutorials

(Romina) #1
Adding a List

Note that you will need to import java.util.List and java.util.ArrayList


as well.


Step #2: Save Adds to List....................................................................


Note that the above code will not compile, because our onSave Button click


handler is still set up to reference the old single restaurant model. For the


time being, we will have onSave simply add a new restaurant.


All we need to do is add a local restaurant r variable, populate it, and add it


to the list:


private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
Restaurant r=new Restaurant();
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);

r.setName(name.getText().toString());
r.setAddress(address.getText().toString());

RadioGroup types=(RadioGroup)findViewById(R.id.types);

switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
r.setType("sit_down");
break;

case R.id.take_out:
r.setType("take_out");
break;

case R.id.delivery:
r.setType("delivery");
break;
}
}
};

At this point, you should be able to rebuild and reinstall the application.


Test it out to make sure that clicking the button does not cause any


unexpected errors.


30
Free download pdf