Android Programming Tutorials

(Romina) #1
Splitting the Tab

Step #3: Get Control On List Events....................................................


Next, we need to detect when the user clicks on one of our restaurants in


the list, so we can update our detail form with that information.


First, add an import for android.widget.AdapterView to LunchList. Then,


create an AdapterView.OnItemClickListener named onListClick:


private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
}
};

Finally, call setOnItemClickListener() on the ListView in the activity's


onCreate() to connect the ListView to the onListClick listener object


(list.setOnItemClickListener(onListClick);)


Step #4: 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);

51
Free download pdf