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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.MediaAdapter 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: MediaAdapter.java,v 8ff4bfa9541b 2010/06/24 23:35:43 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nonnegative;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.bluebill.observation.Observation;
import it.tidalwave.bluebill.observation.ObservationSet;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class MediaAdapter extends BaseAdapter
  {
    private static final String CLASS = MediaAdapter.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final LayoutInflater layoutInflater;

    @Nonnull
    private final ObservationSet observationSet;

    private final int resourceId;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public MediaAdapter (final @Nonnull Context context,
                         final @Nonnull ObservationSet observationSet,
                         final int resourceId)
      {
        this.layoutInflater = LayoutInflater.from(context);
        this.observationSet = observationSet;
        this.resourceId = resourceId;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Observation getItem (final @Nonnegative int index)
      {
        return observationSet.find(Observation.class).results().get(index);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public long getItemId (final @Nonnegative int id)
      {
        return id;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnegative
    public int getCount()
      {
        return observationSet.find(Observation.class).count();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public View getView (final @Nonnegative int index,
                         @CheckForNull View view,
                         final @Nonnull ViewGroup viewGroup)
      {
        MediaViewHelper mediaViewHelper;

        if (view == null)
          {
            view = layoutInflater.inflate(resourceId, null);
            view.setTag(mediaViewHelper = createHelper(view));
          }
        else
          {
            mediaViewHelper = (MediaViewHelper)view.getTag();
          }

        try
          {
            mediaViewHelper.setObservation(getItem(index));
          }
        catch (NotFoundException e)
          {
            e.printStackTrace();
          }
        catch (RuntimeException e)
          {
            e.printStackTrace();
          }

        return view;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean hasStableIds()
      {
        return super.hasStableIds();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected abstract MediaViewHelper createHelper (final @Nonnull View view);
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy