Search code examples
javaandroidandroid-activityparcelable

Pass list of data (object) between activites in Andriod


I have a class Dice implementing Parcelable like below

import android.os.Parcel;
import android.os.Parcelable;


public class Dice implements Parcelable {

    int mDiceNumber;
    boolean mDiceClicked = false;


    public Dice(int mDiceNumber, boolean diceClicked) {
        this.mDiceNumber = mDiceNumber;
        mDiceClicked = diceClicked;
    }

    protected Dice(Parcel in) {
        mDiceNumber = in.readInt();
        mDiceClicked = in.readByte() != 0;
    }

    public static final Creator<Dice> CREATOR = new Creator<Dice>() {
        @Override
        public Dice createFromParcel(Parcel in) {
            return new Dice(in);
        }

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

    public boolean isDiceClicked() {
        return mDiceClicked;
    }

    public void setDiceClicked(boolean diceClicked) {
        mDiceClicked = diceClicked;
    }

    public int getDiceNumber() {
        return mDiceNumber;
    }

    public void setDiceNumber(int diceNumber) {
        mDiceNumber = diceNumber;
    }


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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mDiceNumber);
        dest.writeByte((byte) (mDiceClicked ? 1 : 0));
    }
}

I have also two activities my MainActivity and SecondActivity. In MaiActivity I have an array of my class Dice

private Dice[] mDice = new Dice[]{
            new Dice(1, false),
            new Dice(2, false),
            new Dice(3, false),
            new Dice(4, false),
            new Dice(5, false),
            new Dice(6, false)
    };

I want to be able to pass my whole array of dice to my SecondActivity.

This is what I have done so far:

in MainActivity:

 Intent intent = new Intent(MainActivity.this, SecondActivity.class);
 intent.putExtra("Dices", mDice);
 startActivity(intent);

in SecondActivity:

 Dice mDice[] = getIntent().getParcelableExtra("Dices");

 TextView textView = (TextView) findViewById(R.id.Text1);
 textView.setText(mDice.getDiceNumber());

But it crashes. I know how to pass an object of it was just one object and not a whole array. But whenever I try to pass whole array it crashes. I have also tried with

Dice mDice[] = getIntent().getParcelableArrayExtra()("Dices");
//and also with defining my mDice[] as Parcelable like this
Parcelable mDice[] = getIntent().getParcelableArrayExtra()("Dices");

but with no luck. Please help me!

EDIT:

I tried to change my Dice mDice[] to Parcelable mDice[] and now I am able to open my second activity without crashing the app. However I cannot retrieve my values as my mDice[] does not have the methods mDice[0].getDiceNumber() and mDice[0].isDiceClicked() for some reason.

in SecondActivity:

 Parcelable mDice[] = getIntent().getParcelableExtra("Dices");

 TextView textView = (TextView) findViewById(R.id.Text1);
 textView.setText(mDice[0].getDiceNumber()); // here I get an error saying Cannot resolve method 'getDiceNumber' in 'Parcelable'

Solution

  • Below is the line, where the issue is;

    textView.setText(mDice.getDiceNumber())
    

    It must be like below;

    textView.setText(mDice[0].getDiceNumber())
    

    Edits:

    To answer your updated question,

    You are trying to fetch the required fields from the Parcelable[] array. YOu have to actually cast it to Dice Object.

    Example :

    Parcelable[] dices = 
    getIntent().getExtras().getParcelableArray("Dices");
    Dices[] newDices = new Dice[dices.length];
    for (int i = 0 ; i < dices.length; i++) {
       newDices[i] = (Dice)dices[i];
       newDices[i].getDiceNumber() // This will give you the result
    }