Android Open Source - Netball Positions Activity






From Project

Back to project page Netball.

License

The source code is released under:

GNU General Public License

If you think the Android project Netball listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.prisch.activities;
/* w w  w.ja v a 2s. com*/
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.prisch.R;
import com.prisch.model.*;
import com.prisch.repositories.GameRepository;
import com.prisch.repositories.OpponentRecordRepository;
import com.prisch.repositories.TeamMemberRepository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PositionsActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int TEAM_LOADER = 0;
    private static final int GAME_LOADER = 1;

    private static final Map<Position, Integer> POSITION_BUTTON_MAP = new HashMap<Position, Integer>();
    static {
        POSITION_BUTTON_MAP.put(Position.GS, R.id.button_goalShoot);
        POSITION_BUTTON_MAP.put(Position.GA, R.id.button_goalAttack);
        POSITION_BUTTON_MAP.put(Position.WA, R.id.button_wingAttack);
        POSITION_BUTTON_MAP.put(Position.C, R.id.button_center);
        POSITION_BUTTON_MAP.put(Position.WD, R.id.button_wingDefense);
        POSITION_BUTTON_MAP.put(Position.GD, R.id.button_goalDefense);
        POSITION_BUTTON_MAP.put(Position.GK, R.id.button_goalKeeper);
    }

    private TeamMemberRepository teamMemberRepository;
    private GameRepository gameRepository;
    private OpponentRecordRepository opponentRecordRepository;

    // Map from a Position to the Team assignment ID (Player and Position) currently assigned to that Position
    private Map<Position, Long> positionMap = new HashMap<Position, Long>(7);

    private Long gameId;

    // ===== Lifecycle Methods =====

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.positions);

        teamMemberRepository = new TeamMemberRepository(this);
        gameRepository = new GameRepository(this);
        opponentRecordRepository = new OpponentRecordRepository(this);

        getLoaderManager().initLoader(TEAM_LOADER, null, this);
        getLoaderManager().initLoader(GAME_LOADER, null, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.positions, menu);
        return true;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case TEAM_LOADER:
                return teamMemberRepository.getActiveTeam();
            case GAME_LOADER:
                return gameRepository.getActiveGameLoader();
            default:
                throw new IllegalArgumentException("No Loader registered for " + id);
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        switch (loader.getId()) {
            case TEAM_LOADER:
                positionMap.clear();

                while (data.moveToNext()) {
                    Position position = Position.fromAcronym(data.getString(data.getColumnIndex(TeamMember.POSITION)));
                    Long teamAssignmentId = data.getLong(data.getColumnIndex(TeamMember.ID));
                    String playerName = data.getString(data.getColumnIndex(Player.NAME));
                    configureButton(position, teamAssignmentId, playerName);
                }
                break;
            case GAME_LOADER:
                if (data.moveToNext()) {
                    gameId = data.getLong(data.getColumnIndex(Game.ID));

                    long teamScore = data.getLong(data.getColumnIndex(Game.TEAM_SCORE));
                    long opponentScore = data.getLong(data.getColumnIndex(Game.OPPONENT_SCORE));
                    getActionBar().setTitle(String.format("Score: %d - %d", teamScore, opponentScore));
                }
                break;
            default:
                throw new IllegalArgumentException("No Loader registered for " + loader.getId());
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Nothing to cleanup
    }

    // ===== Event Listeners =====

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_opponentGoal:
                new Thread(new Runnable() {
                    public void run() {
                        opponentRecordRepository.createRecord(new Date(), OpponentAction.GOAL);
                    }
                }).start();
                return true;
            case R.id.menu_substitutions:
                Intent substitutionIntent = new Intent(getApplicationContext(), SubstitutionActivity.class);
                substitutionIntent.putExtra(SubstitutionActivity.GAME_KEY, gameId);
                startActivity(substitutionIntent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // ===== Helper Methods =====

    private void configureButton(final Position position, final Long teamAssignmentId, String playerName) {
        final String BUTTON_FORMAT_PATTERN = "%s<br/><small><small><small>%s</small></small></small>";

        Button button = (Button)findViewById(POSITION_BUTTON_MAP.get(position));
        button.setText(Html.fromHtml(String.format(BUTTON_FORMAT_PATTERN, position.getAcronym(), playerName)));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent actionsIntent = new Intent(getApplicationContext(), ActionsActivity.class);
                actionsIntent.putExtra(ActionsActivity.POSITION_KEY, position);
                actionsIntent.putExtra(ActionsActivity.TEAM_ID_KEY, teamAssignmentId);
                startActivity(actionsIntent);
            }
        });
    }
}




Java Source Code List

com.prisch.activities.ActionsActivity.java
com.prisch.activities.BaseTeamActivity.java
com.prisch.activities.DashboardActivity.java
com.prisch.activities.GameStatsActivity.java
com.prisch.activities.GamesActivity.java
com.prisch.activities.PlayersActivity.java
com.prisch.activities.PositionsActivity.java
com.prisch.activities.SubstitutionActivity.java
com.prisch.activities.TeamActivity.java
com.prisch.content.DatabaseHelper.java
com.prisch.content.NetballContentProvider.java
com.prisch.controls.ActionButton.java
com.prisch.fragments.PositionStatsFragment.java
com.prisch.loaders.GameStatsLoader.java
com.prisch.model.Action.java
com.prisch.model.GameStats.java
com.prisch.model.Game.java
com.prisch.model.OpponentAction.java
com.prisch.model.OpponentRecord.java
com.prisch.model.PlayerStats.java
com.prisch.model.Player.java
com.prisch.model.Position.java
com.prisch.model.Record.java
com.prisch.model.TeamMember.java
com.prisch.repositories.GameRepository.java
com.prisch.repositories.OpponentRecordRepository.java
com.prisch.repositories.PlayerRepository.java
com.prisch.repositories.RecordRepository.java
com.prisch.repositories.StatsRepository.java
com.prisch.repositories.TeamMemberRepository.java
com.prisch.util.DateUtils.java
com.prisch.views.GameAdapter.java
com.prisch.views.GameStatsAdapter.java
com.prisch.views.PlayerStatsAdapter.java
com.prisch.views.PlayerStatsListItem.java
com.prisch.views.TeamAdapter.java