Android Programming Tutorials

(Romina) #1
Splitting the Tab

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.


Step #5: Switch Tabs On Clicks...........................................................


Finally, we want to switch to the details form when the user clicks a


restaurant in the list.


This is just one extra line of code, in the onItemClick() method of our


onListClick listener object:


getTabHost().setCurrentTab( 1 );

This just changes the current tab to the one known as index 1 , which is the


second tab (tabs start counting at 0 ).


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


and test out the new functionality.


52
Free download pdf