TeamList.java :  » Game » compte-les-points » com » moanoit » belote » Android Open Source

Android Open Source » Game » compte les points 
compte les points » com » moanoit » belote » TeamList.java
package com.moanoit.belote;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.moanoit.belote.db.BeloteDbAdapter;

public class TeamList extends ListActivity {
  /**
   * tag pour traces.
   */
  private static final String TAG = "TeamList";
  
  private static final int TEAM_CREATE = 0;
  private static final int TEAM_EDIT = 1;

  private static final int INSERT_ID = Menu.FIRST;
  private static final int DELETE_ID = Menu.FIRST + 1;
  private static final int EDIT_ID = Menu.FIRST + 2;

  /**
   * Connexion  la base de donnes.
   */
  private BeloteDbAdapter mDbHelper;
  /**
   * Numro d'quipe.
   */
  private int mTeamNumber;
  /**
   * Bouton de cration d'quipe.
   */
  private Button mButtonNewTeam;
  private Bundle mExtras;
  private Long mExcludedTeamId;

  /**
   * Called when the activity is first created.
   * @param savedInstanceState saved state
   */
  @Override
  public final void onCreate(Bundle savedInstanceState) {
    try {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list_teams);
      setTitle(R.string.team_list_title);

      mButtonNewTeam = (Button) findViewById(R.id.team_add_team);

      mExtras = getIntent().getExtras();
      if (mExtras != null) {
        mTeamNumber = mExtras.getInt(Constantes.KEY_TEAM);
        if (mExtras.containsKey(Constantes.KEY_EXCLUDE_TEAM)) {
          mExcludedTeamId = 
            mExtras.getLong(Constantes.KEY_EXCLUDE_TEAM);
        }
      } else {
        mTeamNumber = -1;
      }

      // Base de donnes des quipes
      mDbHelper = new BeloteDbAdapter(this);
      mDbHelper.open();

      fillTeamData();
      registerForContextMenu(getListView());
      mButtonNewTeam.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
          createTeam();
        }
      });

    } catch (Exception e) {
      Log.e(TAG, "Error : " + e.getMessage());
    }
  }

  /**
   * Fill the team data.
   */
  private void fillTeamData() {
    // Get all of the rows from the database and create the item list
    Cursor teamsCursor;
    if (mExcludedTeamId != null) {
      teamsCursor = mDbHelper.fetchAllTeams(mExcludedTeamId);
    } else {
      teamsCursor = mDbHelper.fetchAllTeams();
    }
    startManagingCursor(teamsCursor);

    // Create an array to specify the fields we want to display in the list
    // (only NAME)
    String[] from = new String[] { Constantes.KEY_TEAM_NAME,
        Constantes.KEY_TEAM_PLAYER1,
        Constantes.KEY_TEAM_PLAYER2,
        Constantes.KEY_TEAM_PLAYER1,
        Constantes.KEY_TEAM_PLAYER2};

    // and an array of the fields we want to bind those fields to (in this
    // case just text1)
    int[] to = new int[] { R.id.team_list_team,
        R.id.team_list_member1,
        R.id.team_list_member2,
        R.id.team_list_member1,
        R.id.team_list_member2};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter teams = new SimpleCursorAdapter(this,
        R.layout.ligne_team, teamsCursor, from, to);
    setListAdapter(teams);
  }

  @Override
  public final boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_team_add);
    return true;
  }

  @Override
  public final boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case INSERT_ID:
      createTeam();
      return true;
    default:
      // TODO : exception
      break;
    }

    return super.onMenuItemSelected(featureId, item);
  }

  @Override
  public final void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_team_delete);
    menu.add(0, EDIT_ID, 1, R.string.menu_team_edit);
  }

  @Override
  public final boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case DELETE_ID:
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
      .getMenuInfo();
      mDbHelper.deleteTeam(info.id);
      fillTeamData();
      return true;
    case EDIT_ID:
      info = (AdapterContextMenuInfo) item.getMenuInfo();
      editTeam(info.id);
      return true;
    default:
      // TODO : exception
      break;
    }
    return super.onContextItemSelected(item);
  }

  private void createTeam() {
    Intent i = new Intent(this, TeamEdit.class);
    startActivityForResult(i, TEAM_CREATE);
  }

  private void editTeam(long id) {
    Intent i = new Intent(this, TeamEdit.class);
    i.putExtra(Constantes.KEY_TEAM_ROWID, id);
    startActivityForResult(i, TEAM_EDIT);
  }

  @Override
  protected final void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (mTeamNumber != -1) {
      Intent i = new Intent();
      i.putExtras(mExtras);
      // i.putExtra(BeloteCount.KEY_TEAM, mTeamNumber);
      i.putExtra(Constantes.KEY_TEAM_ROWID, id);
      setResult(Constantes.TEAM_EDIT, i);
      finish();
    }
  }

  @Override
  protected final void onActivityResult(int requestCode, int resultCode,
      Intent intent) {
    // super.onActivityResult(requestCode, resultCode, intent);
    fillTeamData();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.