Android Programming Tutorials

(Romina) #1
Making Our List Be Fancy

Some of the unusual attributes applied in this layout include:



  • android:padding, which arranges for some whitespace to be put
    outside the actual widget contents but still be considered part of the


widget (or container) itself when calculating its size



  • android:textStyle, where we can indicate that some text is in bold


or italics



  • android:singleLine, which, if true, indicates that text should not
    word-wrap if it extends past one line

  • android:ellipsize, which indicates where text should be truncated
    and ellipsized if it is too long for the available space


Step #3: Override getView(): The Simple Way..................................


Next, we need to use this layout ourselves in our RestaurantAdapter. To do


this, we need to override getView() and inflate the layout as needed for


rows.


Modify RestaurantAdapter to look like the following:


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

public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;

if (row==null) {
LayoutInflater inflater=getLayoutInflater();

row=inflater.inflate(R.layout.row, null);
}

Restaurant r=model.get(position);

((TextView)row.findViewById(R.id.title)).setText(r.getName());
((TextView)row.findViewById(R.id.address)).setText(r.getAddress());

ImageView icon=(ImageView)row.findViewById(R.id.icon);

40
Free download pdf