Android Open Source - Netball Team Adapter






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.views;
/*from  w ww  .  ja v  a  2  s  .c  o m*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.prisch.R;
import com.prisch.model.Player;
import com.prisch.model.Position;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeamAdapter extends BaseAdapter {

    private static final int HEADER_LAYOUT_ID = R.layout.list_header;
    private static final int PLAYER_LAYOUT_ID = R.layout.list_players;

    private final Context context;
    private final String[] groupsInOrder;
    private final boolean displayHeadings;

    private Map<Long, Position> playerPositionMap = new HashMap<Long, Position>();
    private Map<String, List<Player>> playerGroupMap = new HashMap<String, List<Player>>();

    // ===== Constructor =====

    public TeamAdapter(Context context, String[] groupsInOrder) {
        this(context, groupsInOrder, true);
    }

    public TeamAdapter(Context context, String[] groupsInOrder, boolean displayHeadings) {
        this.context = context;
        this.groupsInOrder = groupsInOrder;
        this.displayHeadings = displayHeadings;
    }

    // ===== Custom Operations =====

    public void putPlayers(String group, List<Player> players) {
        playerGroupMap.put(group, players);
        this.notifyDataSetChanged();
    }

    public void setPlayerPositionMap(Map<Long, Position> playerPositionMap) {
        this.playerPositionMap = playerPositionMap;
        this.notifyDataSetChanged();
    }

    public void clearPlayers() {
        this.playerGroupMap.clear();
        this.notifyDataSetChanged();
    }

    // ===== Adapter Operations =====

    @Override
    public int getCount() {
        int count = 0;

        for (List<Player> players : playerGroupMap.values()) {
            count += players.size();
        }

        if (displayHeadings) {
            count += groupsInOrder.length;
        }

        return count;
    }

    @Override
    public Object getItem(int position) {
        int remaining = position;

        for (String group : groupsInOrder) {
            if (displayHeadings) {
                if (remaining == 0) {
                    return group;
                }

                --remaining; // The group counts as an item
            }

            List<Player> players = playerGroupMap.get(group);
            if (players != null) {
                if (players.size() > remaining) {
                    return players.get(remaining);
                }

                remaining -= players.size(); // The initial position is past this group
            }
        }

        return null; // Index is too large
    }

    @Override
    public long getItemId(int position) {
        Object item = getItem(position);
        if (item instanceof Player) {
            return ((Player)item).getId();
        }
        return -1L;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView;
        final Object item = getItem(position);

        if (item instanceof Player) {
            Player player = (Player)item;

            itemView = layoutInflater.inflate(PLAYER_LAYOUT_ID, parent, false);

            TextView nameText = (TextView)itemView.findViewById(R.id.text_playerName);
            nameText.setText(player.getName());

            if (playerPositionMap.containsKey(player.getId())) {
                TextView positionText = (TextView)itemView.findViewById(R.id.text_playerPosition);
                positionText.setText(playerPositionMap.get(player.getId()).getAcronym());
            }
        } else {
            itemView = layoutInflater.inflate(HEADER_LAYOUT_ID, parent, false);

            TextView headerText = (TextView)itemView.findViewById(R.id.text_header);
            headerText.setText(item.toString());
        }

        return itemView;
    }

}




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