Android Open Source - Briscola Players Select Activity






From Project

Back to project page Briscola.

License

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.

Java Source Code

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() : "");
  }
}




Java Source Code List

com.gmail.craptik.briscola.ActionAdapter.java
com.gmail.craptik.briscola.ActionItem.java
com.gmail.craptik.briscola.CallCardCupsSelectActivity.java
com.gmail.craptik.briscola.CallCardFeathersSelectActivity.java
com.gmail.craptik.briscola.CallCardSuitSelectActivity.java
com.gmail.craptik.briscola.CallCardSunsSelectActivity.java
com.gmail.craptik.briscola.CallCardSwordsSelectActivity.java
com.gmail.craptik.briscola.CalleeSelectActivity.java
com.gmail.craptik.briscola.CallerBidSelectActivity.java
com.gmail.craptik.briscola.Card.java
com.gmail.craptik.briscola.ECardNumber.java
com.gmail.craptik.briscola.ECardSuit.java
com.gmail.craptik.briscola.EGameProgress.java
com.gmail.craptik.briscola.FirstNemesisSelectActivity.java
com.gmail.craptik.briscola.GameData.java
com.gmail.craptik.briscola.IDatabase.java
com.gmail.craptik.briscola.MainActivity.java
com.gmail.craptik.briscola.NewGameActivity.java
com.gmail.craptik.briscola.PlayerAdapter.java
com.gmail.craptik.briscola.Player.java
com.gmail.craptik.briscola.PlayersSelectActivity.java
com.gmail.craptik.briscola.SQLGameDatabase.java
com.gmail.craptik.briscola.ScoreSelectActivity.java
com.gmail.craptik.briscola.TestDatabase.java