@Override <_i public View getView(int position, View convertView, ViewGroup parent) { Review review = this.reviews.get(position);

return new ReviewListView(this.context, review.name, review.rating);

private final class ReviewListView extends LinearLayout {

private TextView name; private TextView rating;

Define custom inner F View class public ReviewListView(Context context, String name, String rating) { super(context);


LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup . LayoutParams . WRAP_CONTENT,

ViewGroup . LayoutParams . WRAP_CONTENT) ; <- Set layout in code params.setMargins(5, 3, 5, 0) ;

this.name = new TextView(context); this.name.setText(name); this.name.setTextSize(16f); this.name.setTextColor(Color.WHITE); this.addView(this.name, params);

this.rating = new TextView(context);


this.rating.setTextSize(16f) ;


this.addView(this.rating, params);


TextView members

The first thing to note in ReviewAdapter is that it extends BaseAdapter O. BaseAdapter is an Adapter implementation that provides basic event-handling support. Adapter itself is an interface in the android.Widget package that provides a way to bind data to a View with some common methods. This is often used with collections of data, such as we saw with Spinner and ArrayAdapter in listing 3.1. Another common usage is with a CursorAdapter, which returns results from a database (something we will see in chapter 5). Here we are creating our own Adapter, because we want it to return a custom View.

Our ReviewAdapter class accepts two parameters in the constructor and sets those values to two simple member objects: Context and List<Review> ©. Then this class goes on to implement the straightforward required Adapter interface methods that return a count, an item, and an ID (we just use the position in the collection as the ID) . The next Adapter method we have to implement is the important one, getView(). This is where the Adapter will return any View we create for a particular item in the collection of data it is supporting. Within this method we get a particular Review object based on the position/ID, and then we create an instance of a custom ReviewListView object to return as the View Q.

ReviewListView itself, which extends LinearLayout (something you will learn more about in section 3.2.4), is an inner class inside ReviewAdapter (since we will never use it outside of returning a view from ReviewAdapter) Q. Within it we see an example of setting layout and View details in code, rather than in XML. Here we set the orientation, parameters, and margin for our layout ©. Then we populate the simple TextView objects that will be children of our new View and represent data Q. Once these are set up via code, we add them to the parent container (in this case the parent is our custom class ReviewListView) ©. This is where the data binding happens—the bridge to the View from data. Another important thing to note about this is that we have created not only a custom View but a composite one as well. That is, we are using simple existing View objects in a particular layout to construct a new type of reusable View, which shows the detail of a selected Review object on screen, as shown in figure 3.2.

Our ReviewListView object, while custom, is admittedly (and intentionally) fairly simple. In many cases you will be able to create custom views by combining existing views in this manner. Nevertheless, you should also be aware that you can go deeper and extend the View class itself. Then you can implement core methods as needed. Using this approach you have access to the lifecycle methods of a View (not an Activity as we have already covered, but an individual View). These include onMeasure(), onLayout(), onDraw(), onVisibilityChanged(), and others. Though we don't need that level of control here, you should be aware that extending View gives you a great deal of power to create custom components.

Now that you have seen how we get the data for our reviews and what the Adapter and custom View we are using look like, the next thing we need to do is take a closer look at a few more aspects of views, including layout.

0 0

Post a comment