Android Open Source - ponyville-live-android Enum Adapter

From Project

Back to project page ponyville-live-android.


The source code is released under:

Apache License

If you think the Android project ponyville-live-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*from w  w w  .j a  va 2 s  .  c o  m*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;

class EnumAdapter<T extends Enum<T>> extends BindableAdapter<T> {
  private final T[] enumConstants;
  private final boolean showNull;
  private final int nullOffset;

  EnumAdapter(Context context, Class<T> enumType) {
    this(context, enumType, false);

  EnumAdapter(Context context, Class<T> enumType, boolean showNull) {
    this.enumConstants = enumType.getEnumConstants();
    this.showNull = showNull;
    this.nullOffset = showNull ? 1 : 0;

  public final int getCount() {
    return enumConstants.length + nullOffset;

  public final T getItem(int position) {
    if (showNull && position == 0) {
      return null;

    return enumConstants[position - nullOffset];

  public final long getItemId(int position) {
    return position;

  public final View newView(LayoutInflater inflater, int position, ViewGroup container) {
    return inflater.inflate(android.R.layout.simple_spinner_item, container, false);

  public final void bindView(T item, int position, View view) {
    TextView tv = ButterKnife.findById(view,;

  public final View newDropDownView(LayoutInflater inflater, int position, ViewGroup container) {
    return inflater.inflate(android.R.layout.simple_spinner_dropdown_item, container, false);

  protected String getName(T item) {
    return String.valueOf(item);

Java Source Code List