Making Our List Be Fancy
address.setText(r.getAddress());
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);
}
}
}
}
Rebuild and reinstall the application, then try adding several restaurants
and confirm that, when the list is scrolled, everything appears as it should –
the name, address, and icon all change.
Note that you may experience a problem, where your EditText widgets
shrink, failing to follow the android:stretchColumns rule. This is a bug in
Android that will hopefully be repaired one day.
Extra Credit.................................................................................................
Here are some things you can try beyond those step-by-step instructions:
- Customize the rows beyond just the icon based on each restaurant,
such as applying different colors to the name based upon certain
criteria.
- Use three different layouts for the three different restaurant types.
To do this, you will need to override getItemViewType() and
getViewTypeCount() in the custom adapter to return the appropriate
data.
Further Reading..........................................................................................
Using custom Adapter classes and creating list rows that are more than
mere strings is covered in the "Getting Fancy with Lists" chapter of The
Busy Coder's Guide to Android Development.