1

I know there have been a lot of questions on this topic but I can't find an answer for my specific case. Basically I have a two dimensional array of a fairly simple class that I need to pass. I am trying to use Parcelable and I ran my class through this to get this:

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

public class Notam implements Parcelable {
public String airfield;
public String identifier;
public String notamText;
public String fromTime;
public String untilTime;
public boolean hidden;

// one constructor
public Notam(String airfieldname, String newIdentifier, String newNotamText, String newFrom, String newUntil) {
    airfield = airfieldname;
    identifier = newIdentifier;
    notamText = newNotamText;
    fromTime = newFrom;
    untilTime = newUntil;
}


public void setHidden(boolean changeHidden) {
    hidden = changeHidden;
}


protected Notam(Parcel in) {
    airfield = in.readString();
    identifier = in.readString();
    notamText = in.readString();
    fromTime = in.readString();
    untilTime = in.readString();
    hidden = in.readByte() != 0x00;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(airfield);
    dest.writeString(identifier);
    dest.writeString(notamText);
    dest.writeString(fromTime);
    dest.writeString(untilTime);
    dest.writeByte((byte) (hidden ? 0x01 : 0x00));
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Notam> CREATOR = new Parcelable.Creator<Notam>() {
    @Override
    public Notam createFromParcel(Parcel in) {
        return new Notam(in);
    }

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

I have tried all manner of things I found from here but I am now lost. At the moment in Main Activity I have

Notam[][] notamList = new Notam[10][100];

Intent intent = new Intent(this, NotamList.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("notamList", notamList);
intent.putExtras(bundle);
startActivity(intent);

And then in the receiving activity:

Notam[][] notamList = new Notam[10][100];

Bundle data = getIntent().getExtras();
notamList = data.getParcelableArrayList("notamList");

But this is not even close to working so I ask for your wisdom and guidance!

Gavin
  • 460
  • 4
  • 11
  • Please clarify what "not even close to working" means. – Egor Nov 23 '13 at 00:20
  • Well I have this error in Main Activity "The method putParcelableArrayList(String, ArrayList extends Parcelable>) in the type Bundle is not applicable for the arguments (String, Notam[][])" and in the receiving activity "Multiple markers at this line - Type mismatch: cannot convert from Notam to Notam[] - Type mismatch: cannot convert from ArrayList to Notam[][]" – Gavin Nov 23 '13 at 00:31

2 Answers2

2

In your MainActivity:

Notam[][] notamList = new Notam[10][100];

Is that the right way to create Notam object? I don't think so. Look at your constructor:

public Notam(String airfieldname, String newIdentifier, String newNotamText, String newFrom, String newUntil) { ... }

Secondly, again in MainActivity, you are creating a bundle and stuffing the Parcelable object into it and then stuffing the bundle into the intent. You can directly put the Parcelable object into intent like this:

intent.putExtras("notamList", correctly_instantiated_Notam_obj);

HTH. PS: You could use java Serializable in case you are familiar with that than using Parcels. PS: You can look at this example

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • I am just initialising the array there. Later I populate it with notamList[Integer.parseInt(params[1])][counter++] = new Notam( params[0], link.text().substring(0, 8), text, from,until); – Gavin Nov 23 '13 at 01:32
  • Also the example you linked it like all others I saw. It is just passing a single class. I am trying to a pass a 2D array of classes. That is my problem? Or should I not be trying to do that and I should be trying to do something else? – Gavin Nov 23 '13 at 02:26
  • You typically send one object at a time. Now, there is nothing preventing you from creating another class that has an ArrayList of your base class Notam or ArrayList of ArrayList's. You could do that or you could create a series of intent.putExtras("notamList_" + row_num + "_" + col_num, correctly_instantiated_Notam_obj); via a nested for loop and unmarshall at the other end appropriately. Left to your creativity. HTH. – VJ Vélan Solutions Nov 24 '13 at 06:06
  • Thanks for that. I was thinking that I might have to do the series of intent.putextras... Not very elegant though and I have the issue of trying to work out how many I have at the other end of each. I guess I could count them as I create them and send that number through as well? I am only new at Java so I haven't used ArrayLists before. I will google around but if anyone wants to tell me how I can bundle up my Notam array into an ArrayList that would be awesome! – Gavin Nov 24 '13 at 06:51
  • You are welcome. If it helped you, pls accept the answer. Hint: ArrayList notamRow = new ArrayList(); notamRow.add(correctly_instantiated_Notam_obj); And then ArrayList> notamCol = new ArrayList>(); notamCol.add(notamRow); So, notamCol would be your equivalent of Notam[][]. If you want to this 2D object to persist and be available for all other activities or components in your app, then you could consider using Sqlite or just simply storing to the file system or even just use a Singleton instance to store/restore this 2D data. HTH. – VJ Vélan Solutions Nov 24 '13 at 12:56
0

As far as I can tell this is not possible. You need to create a single arraylist and pass that through as per the comments.

Gavin
  • 460
  • 4
  • 11