Android Programming Tutorials

(Romina) #1
Getting More Active

Then, we need to add all our logic for accessing the various form widgets,


plus an onSave listener for our Save button, plus all necessary imports.


Set the import list for DetailForm to be:


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

Then, add the following data members to the DetailForm class:


EditText name=null;
EditText address=null;
EditText notes=null;
RadioGroup types=null;
RestaurantHelper helper=null;

Then, copy the widget finders and stuff from onCreate() in LunchList into


the same spot in DetailForm:


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);

Button save=(Button)findViewById(R.id.save);

save.setOnClickListener(onSave);

Finally, add the onSave listener object with a subset of the implementation


from LunchList:


private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;

switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";

113
Free download pdf