示例#1
0
  @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;
    }
  }