Adapter that simply returns row views from a list. : ListActivity « UI « Android






Adapter that simply returns row views from a list.

   
//package pl.ppsoft.fixlang.common;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * Adapter that simply returns row views from a list.
 *
 * If you supply a size, you must implement newView(), to
 * create a required view. The adapter will then cache these
 * views.
 *
 * If you supply a list of views in the constructor, that
 * list will be used directly. If any elements in the list
 * are null, then newView() will be called just for those
 * slots.
 *
 * Subclasses may also wish to override areAllItemsEnabled()
 * (default: false) and isEnabled() (default: false), if some
 * of their rows should be selectable.
 *
 * It is assumed each view is unique, and therefore will not
 * get recycled.
 *
 * Note that this adapter is not designed for long lists. It
 * is more for screens that should behave like a list. This
 * is particularly useful if you combine this with other
 * adapters (e.g., SectionedAdapter) that might have an
 * arbitrary number of rows, so it all appears seamless.
 */
public class ListViewAdapter extends BaseAdapter {
  private List<View> views=null;

  /**
    * Constructor creating an empty list of views, but with
    * a specified count. Subclasses must override newView().
    */
  public ListViewAdapter(int count) {
    super();
    
    views=new ArrayList<View>(count);
    
    for (int i=0;i<count;i++) {
      views.add(null);
    }
  }

  /**
    * Constructor wrapping a supplied list of views.
    * Subclasses must override newView() if any of the elements
    * in the list are null.
    */
  public ListViewAdapter(List<View> views) {
    super();
    
    this.views=views;
  }

  /**
    * Get the data item associated with the specified
    * position in the data set.
    * @param position Position of the item whose data we want
    */
  @Override
  public Object getItem(int position) {
    return(views.get(position));
  }

  /**
    * How many items are in the data set represented by this
    * Adapter.
    */
  @Override
  public int getCount() {
    return(views.size());
  }

  /**
    * Returns the number of types of Views that will be
    * created by getView().
    */
  @Override
  public int getViewTypeCount() {
    return(getCount());
  }

  /**
    * Get the type of View that will be created by getView()
    * for the specified item.
    * @param position Position of the item whose data we want
    */
  @Override
  public int getItemViewType(int position) {
    return(position);
  }

  /**
    * Are all items in this ListAdapter enabled? If yes it
    * means all items are selectable and clickable.
    */
  @Override
  public boolean areAllItemsEnabled() {
    return(false);
  }

  /**
    * Returns true if the item at the specified position is
    * not a separator.
    * @param position Position of the item whose data we want
    */
  @Override
  public boolean isEnabled(int position) {
    return true;
  }

  /**
    * Get a View that displays the data at the specified
    * position in the data set.
    * @param position Position of the item whose data we want
    * @param convertView View to recycle, if not null
    * @param parent ViewGroup containing the returned View
    */
  @Override
  public View getView(int position, View convertView,
                      ViewGroup parent) {
    View result=views.get(position);
    
    if (result==null) {
      result=newView(position, parent);
      views.set(position, result);
    }
    
    return(result);
  }

  /**
    * Get the row id associated with the specified position
    * in the list.
    * @param position Position of the item whose data we want
    */
  @Override
  public long getItemId(int position) {
    return(position);
  }

  /**
    * Create a new View to go into the list at the specified
    * position.
    * @param position Position of the item whose data we want
    * @param parent ViewGroup containing the returned View
    */
  protected View newView(int position, ViewGroup parent) {
    throw new RuntimeException("You must override newView()!");
  }
}

   
    
    
  








Related examples in the same category

1.A list view example where the data for the list comes from an array of strings.
2.A list view example where the data comes from a cursor.
3.Using ListActivity
4.SimpleCursorAdapter and ListActivity
5.Get ListActivity selected index
6.Multi-column ListActivity
7.A list view example with separators.
8.List item click event
9.Dynamic List item
10.Self Wrapper for List
11.Static text for List view
12.List selection event
13.Simple List single choice
14.To do list app
15.Scale listener
16.Weather List Widget
17.Demonstrates expandable lists backed by Cursors
18.Demonstrates expandable lists backed by a Simple Map-based adapter
19.Demonstrates the using a list view in transcript mode
20.Demonstrates how a list can avoid expensive operations during scrolls or flings.
21.Demonstrates how to write an efficient list adapter.
22.A list view where the last item the user clicked is placed in the "activated" state, causing its background to highlight.
23.Calculate the minimum and maximum values out of a list of doubles.
24.Bounded Linked List
25.Diary app