Back to project page Briscola.
The source code is released under:
GNU General Public License
If you think the Android project Briscola listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gmail.craptik.briscola; /* w ww.jav a 2 s .c om*/ import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class PlayersSelectActivity extends Activity { private ListView allPlayersListView; private ArrayList<Player> allPlayers; private ListView activePlayersListView; private ArrayList<Player> activePlayers; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_players_select); } @Override public void onResume() { super.onResume(); allPlayers = (new TestDatabase()).getAllPlayers(); allPlayersListView = (ListView) findViewById(R.id.players_select_all_list); allPlayersListView.addFooterView(new View(this), null, true); allPlayersListView.setAdapter(new PlayerAdapter(this, allPlayers, true)); allPlayersListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Player player = (Player) parent.getItemAtPosition(position); addPlayer(player); } }); activePlayers = new ArrayList<Player>(NewGameActivity.getGameData().getPlayers()); activePlayersListView = (ListView) findViewById(R.id.players_select_active_list); activePlayersListView.setAdapter(new PlayerAdapter(this, activePlayers, false)); activePlayersListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Player player = (Player) parent.getItemAtPosition(position); removePlayer(player); } }); updatePlayerTable(); } public void confirm(View view) { NewGameActivity.getGameData().setPlayers(activePlayers); finish(); } private void addPlayer(Player player) { if(activePlayers.size() < 5) { if(!activePlayers.contains(player)) { activePlayers.add(player); ((BaseAdapter) activePlayersListView.getAdapter()).notifyDataSetChanged(); updatePlayerTable(); } else { Toast.makeText(this, getResources().getString(R.string.player_already_playing), Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, getResources().getString(R.string.too_many_players), Toast.LENGTH_SHORT).show(); } } private void removePlayer(Player player) { if(activePlayers.remove(player)) { ((BaseAdapter) activePlayersListView.getAdapter()).notifyDataSetChanged(); updatePlayerTable(); } else { // Never try to remove a player not in the list... assert false; } } private void updatePlayerTable() { ((TextView) findViewById(R.id.players_select_table_first )).setText(activePlayers.size() > 0 ? activePlayers.get(0).getName() : ""); ((TextView) findViewById(R.id.players_select_table_second )).setText(activePlayers.size() > 1 ? activePlayers.get(1).getName() : ""); ((TextView) findViewById(R.id.players_select_table_third )).setText(activePlayers.size() > 2 ? activePlayers.get(2).getName() : ""); ((TextView) findViewById(R.id.players_select_table_fourth )).setText(activePlayers.size() > 3 ? activePlayers.get(3).getName() : ""); ((TextView) findViewById(R.id.players_select_table_fifth )).setText(activePlayers.size() > 4 ? activePlayers.get(4).getName() : ""); } }