Android removes and adds to ArrayList between actions

I am making my first Android app and here where I am stuck. I have activity A, which requires 4 players. I turn to PickPlayer activities 1, 2, 3, 4in accordance with what a player I want to fill.

ImageButton addp1 = (ImageButton)findViewById(R.id.player1);
addp1.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        Intent i = new Intent(getApplicationContext(), PickPlayer.class);
        i.putExtra("playersList", playersList);
        startActivityForResult(i, 1);
    }
});

In action PickPlayer, I have a list that populates, and each element gets a listener.

    final ArrayList<Player> playersList = (ArrayList<Player>)getIntent().getSerializableExtra("playersList");
    lv.setAdapter(new PlayerItemAdapter(this, android.R.layout.simple_list_item_1, playersList));
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
            player = playersList.get(position);
            playersList.remove(position);
            Intent intentMessage = new Intent();
            intentMessage.putExtra("player", player);
            intentMessage.putExtra("playersList", playersList);
            setResult(RESULT_OK, intentMessage);
            finish();
        }
    });`

All of the above works fine, creating playersListin action A and each time moving from one to another, and removing the player from the list of players when pressed. The problem is that the player is selected by mistake, he needs to be returned to the list again when he is replaced by someone else.

? , , - A PickPlayer ( ) playerList, , . android, . ( db).

+4
3

, , .

arrayList . , player1, player2, player3 .. ..

, , , arrayList, , .

0

IMO, ( - , stackoverflow).

, , . , , :

  • List<Player> getPickedPlayers()
  • List<Player> getNotPickedPlayers()
  • void setPlayerPicked(Player player)
  • void setPlayerNotPicked(Player player)
  • ..

, !

+3

Intent (i.putExtra("playersList", playersList);), ( ). , 2 ArrayList ( ). A B, .

, .

+2

All Articles