Update Our Restaurant Form On Clicks
Next, now that we have control in a list item click, we need to actually find the associated restaurant and update our details form. To do this, you need to do two things. First, move the name, address, and types variables into data members and populate them in the activity's onCreate() – our current code has them as local variables in the onSave listener object's onClick() method. So, you should have some data members like:
EditText name=null;
EditText address=null;
RadioGroup types=null;
And some code after the call to setContentView() in onCreate() like:
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.addr);
types=(RadioGroup)findViewById(R.id.types);
Then, add smarts to onListClick to update the details form:
private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
Restaurant r=model.get(position);
name.setText(r.getName());
address.setText(r.getAddress());
if (r.getType().equals("sit_down")) {
types.check(R.id.sit_down);
}
else if (r.getType().equals("take_out")) {
types.check(R.id.take_out);
}
else {
types.check(R.id.delivery);
}
}
};
Note how we find the clicked-upon restaurant via the position parameter, which is an index into our ArrayList of restaurants.
EditText name=null;
EditText address=null;
RadioGroup types=null;
And some code after the call to setContentView() in onCreate() like:
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.addr);
types=(RadioGroup)findViewById(R.id.types);
Then, add smarts to onListClick to update the details form:
private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
Restaurant r=model.get(position);
name.setText(r.getName());
address.setText(r.getAddress());
if (r.getType().equals("sit_down")) {
types.check(R.id.sit_down);
}
else if (r.getType().equals("take_out")) {
types.check(R.id.take_out);
}
else {
types.check(R.id.delivery);
}
}
};
Note how we find the clicked-upon restaurant via the position parameter, which is an index into our ArrayList of restaurants.
No comments:
Post a Comment