Android Programming Tutorials

(Romina) #1
Making Our List Be Fancy

Step #1: Create a Stub Custom Adapter..............................................


First, let us create a stub implementation of a RestaurantAdapter that will be


where we put our logic for creating our own custom rows. That can look


like this, implemented as an inner class of LunchList:


class RestaurantAdapter extends ArrayAdapter<Restaurant> {
RestaurantAdapter() {
super(LunchList.this,
android.R.layout.simple_list_item_1,
model);
}
}

We hard-wire in the android.R.layout.simple_list_item_1 layout for now,


and we get our Activity and model from LunchList itself.


We also need to change our adapter data member to be a RestaurantAdapter,


both where it is declared and where it is instantiated in onCreate(). Make


these changes, then rebuild and reinstall the application and confirm it


works as it did at the end of the previous tutorial.


Step #2: Design Our Row.....................................................................


Next, we want to design a row that incorporates all three of our model


elements: name, address, and type. For the type, we will use three icons,


one for each specific type (sit down, take-out, delivery). You can use


whatever icons you wish, or you can get the icons used in this tutorial from


the tutorial ZIP file that you can download. They need to be named


ball_red.png, ball_yellow.png, and ball_green.png, all located in


res/drawable/ in your project.


NOTE: If your project has no res/drawable/ directory, but does have


res/drawable-ldpi/ and others with similar suffixes, rename res/drawable-


mdpi/ to res/drawable/ directory for use in this project, and delete the other


res/drawable-* directories.


38
Free download pdf