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.
You will note that we are not adding the actual restaurant to anything – r is a local variable and so goes out of scope after onClick() returns. We will address this shortcoming later in this exercise.
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.
You will note that we are not adding the actual restaurant to anything – r is a local variable and so goes out of scope after onClick() returns. We will address this shortcoming later in this exercise.
No comments:
Post a Comment