All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.android.ui.DisplayableArrayAdapter Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: DisplayableArrayAdapter.java,v f41afde5ae70 2010/06/08 14:47:38 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.android.ui;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.List;
import it.tidalwave.util.As;
import it.tidalwave.util.Displayable;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class DisplayableArrayAdapter extends ArrayAdapter
  {

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int resource,
                                    final int textViewResourceId,
                                    final @Nonnull List objects)
      {
        super(context, resource, textViewResourceId, objects);
      }

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int textViewResourceId,
                                    final @Nonnull List objects)
      {
        super(context, textViewResourceId, objects);
      }

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int resource,
                                    final int textViewResourceId,
                                    final @Nonnull T[] objects)
      {
        super(context, resource, textViewResourceId, objects);
      }

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int textViewResourceId,
                                    final @Nonnull T[] objects)
      {
        super(context, textViewResourceId, objects);
      }

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int resource,
                                    final int textViewResourceId)
      {
        super(context, resource, textViewResourceId);
      }

    public DisplayableArrayAdapter (final @Nonnull Context context,
                                    final int textViewResourceId)
      {
        super(context, textViewResourceId);
      }

    @Override @Nonnull
    public View getView (final @Nonnegative int position,
                         final @Nonnull View convertView,
                         final @Nonnull ViewGroup parent)
      {
        return fixedView(position, super.getView(position, convertView, parent));
      }

    @Override
    public View getDropDownView (int position, View convertView, ViewGroup parent)
      {
        return fixedView(position, super.getDropDownView(position, convertView, parent));
      }

//    @Override TODO
//    public void sort(Comparator comparator)
//      {
//        super.sort(comparator);
//      }

    @Nonnull
    private View fixedView (final @Nonnegative int position,
                            final @Nonnull View view)
      {
        if (view instanceof TextView)
          {
            final Object item = getItem(position);
            Displayable displayable = null;
            
            if (item instanceof Displayable)
              {
                displayable = (Displayable)item;
              }
            else if (item instanceof As)
              {
                displayable = ((As)item).as(Displayable.class);
              }

            ((TextView)view).setText((displayable != null) ? displayable.getDisplayName() : "???");
          }

        return view;
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy