TextView selection;

 String[] items={'lorem', 'ipsum', 'dolor', 'sit', 'amet',

  'consectetuer', 'adipiscing', 'elit', 'morbi', 'vel',

  'ligula', 'vitae', 'arcu', 'aliquet', 'mollis',

  'etiam', 'vel', 'erat', 'placerat', 'ante',

  'porttitor', 'sodales', 'pellentesque', 'augue',

  'purus'};

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  setContentView(R.layout.main);

  setListAdapter(new ArrayAdapterString (this,

   android.R.layout.simple_list_item_1, items));

  selection = (TextView)findViewById(R.id.selection);

 }

 public void onListItemClick(ListView parent, View v,

  int position, long id) {

  selection.setText(items[position]);

 }

}

Things get a wee bit challenging when you realize that in everything up to this point in this chapter, never were we actually changing the ListView itself. All our work was with the adapters, overriding getView() and inflating our own rows and whatnot.

So if we want RateListView to take in any ordinary ListAdapter and “just work,” putting checkboxes on the rows as needed, we are going to need to do some fancy footwork. Specifically, we are going to need to wrap the “raw” ListAdapter in some other ListAdapter that knows how to put the checkboxes on the rows and track the state of those checkboxes.

First we need to establish the pattern of one ListAdapter augmenting another. Here is the code for AdapterWrapper, which takes a ListAdapter and delegates all of the interface’s methods to the delegate (from the FancyLists/RateListView sample project at http://apress.com/):

public class AdapterWrapper implements ListAdapter {

 ListAdapter delegate = null;

 public AdapterWrapper(ListAdapter delegate) {

  this.delegate = delegate;

 }

 public int getCount() {

  return(delegate.getCount());

 }

 public Object getItem(int position) {

  return(delegate.getItem(position));

 }

 public long getItemId(int position) {

  return(delegate.getItemId(position));

 }

 public View getView(int position, View convertView,

  ViewGroup parent) {

  return(delegate.getView(position, convertView, parent));

 }

 public void registerDataSetObserver(DataSetObserver observer) {

  delegate.registerDataSetObserver(observer);

 }

 public boolean hasStableIds() {

  return(delegate.hasStableIds());

 }

 public boolean isEmpty() {

  return(delegate.isEmpty());

 }

 public int getViewTypeCount() {

  return(delegate.getViewTypeCount());

 }

 public int getItemViewType(int position) {

  return(delegate.getItemViewType (position));

 }

 public void unregisterDataSetObserver(DataSetObserver observer) {

  delegate.unregisterDataSetObserver(observer);

 }

 public boolean areAllItemsEnabled() {

  return(delegate.areAllItemsEnabled());

 }

 public boolean isEnabled(int position) {

  return(delegate.isEnabled(position));

 }

}

We can then subclass AdapterWrapper to create RateableWrapper,

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату