@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    dbAdapter.open();
    View vi = convertView;
    ViewHolder holder;

    Episode episode = episodes.get(position);
    int type = getItemWatchedType(episode.ID);

    if (convertView == null) {

      holder = new ViewHolder();

      vi = inflater.inflate(R.layout.season_episode_item, null);

      holder.itemRow = (RelativeLayout) vi.findViewById(R.id.episode_row);
      holder.title = (TextView) vi.findViewById(R.id.episode_title);
      holder.image = (ImageView) vi.findViewById(R.id.episode_image);
      holder.episodeSeasonNumber = (TextView) vi.findViewById(R.id.episode_season_number);
      holder.firstAired = (TextView) vi.findViewById(R.id.episode_airdate);
      holder.clock = (ImageView) vi.findViewById(R.id.episode_clock);

      holder.watchedBtn = (CheckBox) vi.findViewById(R.id.season_episode_checkbox);
      holder.watchedBtn.setOnClickListener(this);
      holder.watchedBtn.setTag(position);

      vi.setTag(holder);
      vi.setOnCreateContextMenuListener(this);
      vi.setOnClickListener(this);

    } else {
      holder = (ViewHolder) vi.getTag();
      ((ViewHolder) vi.getTag()).watchedBtn.setTag(position);
    }
    holder.title.setTag(position);
    holder.title.setText(episode.TITLE);

    // Log.d("SeasonAdapter", episode.IMAGE_URL);

    // Season number and episode number i.e. 'S01E10'
    String s =
        episode.SEASON_NUMBER < 10 ? "0" + episode.SEASON_NUMBER : episode.SEASON_NUMBER + "";
    String e =
        episode.EPISODE_NUMBER < 10 ? "0" + episode.EPISODE_NUMBER : episode.EPISODE_NUMBER + "";
    holder.episodeSeasonNumber.setText("S" + s + "E" + e);

    // Reminder Clock
    Calendar currentTime = Calendar.getInstance();
    currentTime.add(Calendar.DAY_OF_MONTH, -1);
    Calendar episodeTime = getDate(episode);
    holder.clock.setImageResource(
        episodeTime == null
            ? R.drawable.clock_null
            : (getDate(episode).after(currentTime) ? R.drawable.clock : R.drawable.clock_null));

    // CheckBox
    holder.watchedBtn.setClickable(dbAdapter.isSeriesFavorited(episode.SERIES_ID));
    switch (type) {
      case TYPE_WATCHED:
        holder.watchedBtn.setChecked(true);
        break;
      case TYPE_NOT_WATCHED:
        holder.watchedBtn.setChecked(false);
        break;
    }

    holder.firstAired.setText(dateFormat(episode.FIRST_AIRED));

    /** Image Stuff */
    String imgUri = ServerUrls.getImageUrl(activity, ServerUrls.fixURL(episode.IMAGE_URL));
    if (!MemoryCacheUtil.findCachedBitmapsForImageUri(imgUri, imageLoader.getMemoryCache())
        .isEmpty()) {

      imageLoader.displayImage(imgUri, holder.image, this.optionsWithoutDelay);

    } else {

      imageLoader.displayImage(imgUri, holder.image, this.optionsWithDelay);
    }

    dbAdapter.close();
    return vi;
  }
  @SuppressWarnings("unchecked")
  @Override
  public void onClick(View v) {

    Gson gson = new Gson();

    int position;
    switch (v.getId()) {
      case R.id.season_episode_item:
        Intent i = new Intent(activity.getApplicationContext(), SeasonEpisodeActivity.class);
        position = (Integer) v.findViewById(R.id.episode_title).getTag();
        i.putExtra(SeasonEpisodeActivity.EPISODE, episodes.get(position));
        i.putExtra(SeasonEpisodeActivity.SERIES, gson.toJson(series));
        activity.startActivity(i);
        break;

      case R.id.season_episode_checkbox:

        // Get Position from View
        position = (Integer) v.getTag();

        // Open Database
        dbAdapter.open();

        // Get the CheckBox View
        CheckBox checkbox = (CheckBox) v;

        // Watched Helper
        WatchedHelper wHelper = new WatchedHelper(activity.getApplicationContext());

        if (wHelper.isWatched(episodes.get(position).ID)) {

          // Episode has been watched: TRUE, toggle check to: FALSE
          checkbox.setChecked(false);

          // Remove from Watched table and handler Pending
          wHelper.removeWatched(episodes.get(position));

          // Remove from the local ArrayList of things to be displayed
          watched.remove(episodes.get(position).ID);

          // Remove the episode from the Queue DB and check if there is an earlier episode and add
          // to Queue DB
          if (dbAdapter.isInQueue(episodes.get(position).ID)) {
            // Episode removed was in the Queue DB
            dbAdapter.deleteQueueEpisode(episodes.get(position).ID);
            // Log.d(TAG, "Episode " + episodes.get(position).ID + " removed from the Queue DB");

            // Check Previous Watched Episodes and add the latest one, if any
            for (int j = position; j >= 0; j--) {
              // Start at POSITION and work back
              if (dbAdapter.isEpisodeWatched(episodes.get(j).ID)) {
                dbAdapter.insertQueue(episodes.get(j));
                // Log.d(TAG, "Episode " + episodes.get(j).ID + " added to the Queue DB");
                break;
              }
            }
          }

        } else {

          // Episode has not been watched, set to TRUE
          checkbox.setChecked(true);

          // Mark Episode as Watched and handler Pending
          wHelper.markWatched(episodes.get(position).ID);

          // Add to the local ArrayList of items to be displayed as watched
          watched.add(episodes.get(position).ID);
        }
        notifyDataSetChanged();
        dbAdapter.close();
        break;
    }
  }
 public void getWatched() {
   // Open the database and fetch all the watched episodes with that Series ID
   dbAdapter.open();
   this.watched = dbAdapter.fetchWatchedBySeriesId(SERIES_ID);
   dbAdapter.close();
 }