4

I'm making my first android app and here's where i'm stuck. I have an activity A which requires 4 players to be picked. I'm passing to the activity PickPlayer 1,2,3,4 according to which 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);
    }
});

On the PickPlayer activity i have a list which is populated and each item receives 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();
        }
    });`

The above works fine by creating the playersList on activity A and passing it through from one to another each time and removing the player from the playerList on click. Problem is if a player is chosen by mistake he needs to be put back into the list again once replaced by someone else.

Any suggestions on implementing this ? One way i thought of is to pass from activity A to PickPlayer the player ( if one is assigned already at his position ) and readding him to the playerList again but i'm sure there's a better way for it. I'm new to android so i have no idea about resources and best practises. ( example passing an object through activities or an id and run a db query).

Thanks

LefterisL
  • 1,132
  • 3
  • 17
  • 37

3 Answers3

3

IMO, you'll achieve best results with a singleton class (or methods on Application instance - here a good stackoverflow question about it).

Your array would be an internal member of the singleton and have an boolean attribute to indicate if a player is already picked or not. Some methods using this attribute can be implemented like:

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

Hope it helps!

Community
  • 1
  • 1
ARNeto
  • 441
  • 4
  • 8
2

When you send object through an Intent's bundle (i.putExtra("playersList", playersList);), it is marshalled and then unmarshalled on the other side (the new activity). This mean you have 2 instances of ArrayList and its content (one in each activity). If you wish to share data between activity A and activity B, I suggest you store it on an Application instance or by using a singleton.

If your data is coming from a database, you can pass the id through the intent, and get the list of players and the special player with a database query.

Quanturium
  • 5,698
  • 2
  • 30
  • 36
  • It is coming from a database but using that method i still have to pass the list front and back right ? I'm looking for any alternatives. PS: Would you suggest passing a player `object` to an activity or pass the id and then run a query to get the players info ( `object` ) – LefterisL Feb 26 '15 at 20:28
  • If you need the data from the database in both activities, I would suggest either making it a singleton or querying the data in both activities. Regarding the player instance, both solutions are okay as long as a Player is not a big object that will take a long time to marshal. – Quanturium Feb 26 '15 at 20:30
0

Not sure if this is the best way to accomplish this but i'm going to share with you.

I move the arrayList with the players back and forth in between the activities. Once the player is sent back it's removed from it and kept in an object player1,player2,player3 etc etc.

So if the user clicks the button that has already a player assigned to it i simply add that player again into the list and pass the arrayList as i would do if it was empty.

LefterisL
  • 1,132
  • 3
  • 17
  • 37