简体   繁体   中英

Passing class ArrayList between intents

I've created a class named "Player".

public class Player{
    public String name;
    public int score;
}

Everytime I click a button a new TextView is generated and also a new Player class. This is the code:

private TextView createNewTextView (String text){
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView newTextView = new TextView(this);

    newTextView.setLayoutParams(lparams);
    newTextView.setText(text);

    Player newPlayer = new Player();
    newPlayer.name = text;
    newPlayer.score = 0;
    players.add(newPlayer);
    zacniIgro.putExtra("players", (ArrayList<Player>) players);
    return newTextView;
}

Is this the right way to do it? I think there is a small flaw because each time a button is clicked, a new Player is added under the label of "newPlayer". How do I fix this?

And my main problem is this; how do I "unpack" the ArrayList in the second activity so that I can then manipulate with each element of the ArrayList? I tried getStringArrayListExtra() but it doesn't work. I also tried getExtras() but that also doesn't work since it retrieves a bundle. Or is that the right way? But what do I do with the bundle then?

The "label" newPlayer is only the name of a local variable which is always a different thing each time the method is run, so no problem here.

When you call putExtra() here players is interpreted as Serializable so corresponding call is getSerializableExtra("players") . Result must then be casted to an ArrayList<Player> .

To make this work you must additionally make Player class serializable. Here you can just implement interface Serializable .

The better way to do this is with Parcelable .

Simple example:

public class Device implements Parcelable {

private String name;
private String hash;

/**
 * Default private constructor.
 */
public Device() {

}

public Device(final Parcel parcel) {
    super();

    readFromParcel(parcel);
}

public static final Parcelable.Creator<Device> CREATOR = new Parcelable.Creator<Device>() {

    @Override
    public Device createFromParcel(final Parcel source) {
        return new Device(source);
    }

    @Override
    public Device[] newArray(final int size) {
        return new Device[size];
    }
};

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

@Override
public void writeToParcel(final Parcel parcel, final int flags) {
    parcel.writeString(name);
    parcel.writeString(hash);
}

private void readFromParcel(final Parcel parcel) {
    this.name = parcel.readString();
    this.hash = parcel.readString();
}

Put your object in intent like this:

intent.putParcelableArrayListExtra("Key", YourObject);

To Get in another screen:

Intent intent = getIntent();
intent.getParcelableArrayExtra("key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM