0

Im fighting with this some time and still don't know how to make it work. So i have class Player with constructor

Player(String playerName, double playerCash)
{
    this.playerName = playerName;
    this.playerCash = playerCash;
}

In MainActivity i make a player object

Player player = new Player("player", 100);

And now in TextView in SecondActivity i would like to use

playerCash = (TextView) findViewById(R.id.playerCash);
    playerCash.setText(player.getPlayerCash());

Can someone explain me how i can make it works? I get cannot resolve symbol player. Thanks in advance

Rasta_Man
  • 19
  • 4
  • Possible duplicate of [What's the best way to share data between activities?](http://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) – Bojan Dević Feb 21 '16 at 22:43

2 Answers2

0

I am unsure if what you have posted is your entire class, or if you obviated a part of it. I will write this answer, assuming that is your entire class.

So, here is the deal: you need to create methods in your class for you to get and set data to/from it.

public class Player() implements Parcelable {

    private String playerName;
    private String playerCash;

    public Player(String playerName, String playerCash) {
        this.playerName = playerName;
        this.playerCash = playerCash;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public String getPlayerCash() {
        return playerCash;
    }

    public void setPlayerCash(String playerCash) {
        this.playerCash = playerCash;
    }

    public Player(Parcel in) {
        String[] data = new String[2];
        in.readStringArray(data);
        this.playerData = data[0];
        this.playerCash = data[1];
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray(new String[] {this.playerName,
                                           this.playerCash});
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Player createFromParcel(Parcel in) {
           return new Player(in); 
       }

       public Player[] newArray(int size) {
           return new Player[size];
       }
    };
}

edit

I didn't read the part where you say you want to share it between Activities. I only noticed it because someone mentioned implementing Parcelable, which does help your problem.

Anyway, I edited my code to implement it.

To share data between Activities you will also need an intent, and there you can share the data stored on your Player class:

Intent i = new Intent();
i.putExtra("player", new Player("Jhon", "Over 9000!");

And in your 2nd Activity, to get it you would do:

Bundle b = getIntent().getExtras();
Player player = b.getParcelable("player");

Hope that helps.

herrmartell
  • 3,057
  • 4
  • 18
  • 27
0

You can implement Parcelable interface for Player class and then pass instance of it from the MainActivity through Intent to the SecondActivity.

Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46