Android Programming Tutorials

(Romina) #1
Making Our List Be Fancy

if (r.getType().equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);
}
else if (r.getType().equals("take_out")) {
icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}

return(row);
}
}

Notice how we create a row only if needed, recycling existing rows. But, we


still pick out each TextView and ImageView from each row and populate it


from the restaurant at the indicated position.


Step #4: Create a RestaurantHolder....................................................


To improve performance and encapsulation, we should move the logic that


populates a row from a restaurant into a separate class, one that can cache


the TextView and ImageView widgets.


To do this, add the following static inner class to LunchList:


static class RestaurantHolder {
private TextView name=null;
private TextView address=null;
private ImageView icon=null;
private View row=null;

RestaurantHolder(View row) {
this.row=row;

name=(TextView)row.findViewById(R.id.title);
address=(TextView)row.findViewById(R.id.address);
icon=(ImageView)row.findViewById(R.id.icon);
}

void populateFrom(Restaurant r) {
name.setText(r.getName());
address.setText(r.getAddress());

if (r.getType().equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);

41
Free download pdf