简体   繁体   中英

Serializing Complex Objects on Android (Java)

What I'm trying to do is send an object through Intents; I've been looking at implementing Serializable like so:

Intent viewRecipe = new Intent(t, RecipeView.class);
Bundle input = new Bundle();
input.putSerializable("myRecipe", recipes.ReturnRecipe(position).toString());
viewRecipe.putExtras(input);
startActivity(viewRecipe);

However, I'm guessing because I have a complex class it isn't working. Can anyone help explain how i would be able to convert this object to a Serialized string? Also it would be helpful if you know of an easier/simpler way of doing it; may also be worth noting I plan to integrate it with a SQL database further down the line, but I'm stuck at the moment.

The class is:

public class Recipe implements Serializable {

int mRating;
String mName;
String mDescription;

ArrayList<Ingredient> ingredients;
ArrayList<Step> instructions;
...
}

I'm not sure if i have to override "readObject" and "writeObject", or even how I would start to do that. As a side note, Ingredient and Step are not complex classes:

public class Ingredient {

String mName;
int mValue;
String mMeasurement;
    ...
}

public class Step {

int mIndex;
String task;
Date time;
    ...
}

To cut a long story short, I have no idea what I'm doing and could use some guidence. I've read a fair bit on how to serialize simple classes but they haven't really helped.

EDIT: Including the error im getting, just imeplementing Serilizable gets me this error when i retrieve the object from the second acitivity.

Intent myIntent = getIntent();
    Bundle extras = myIntent.getExtras(); //breaks on this line

    currentRecipe = (Recipe) extras.get("myRecipe");

02-29 15:15:56.521: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pocket.recipes/com.pocket.recipes.RecipeView}: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Above is the line shown after "FATAL EXCEPTION: main"

I then have a lot of errors merely saying "at android.app...."

I also have this line in there:

02-29 15:15:56.521: E/AndroidRuntime(534): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Sorry about this, I'm new to Java / Android

Unless you can't avoid it, you shouldn't use Serializable when sending data via Intents (due to performance concerns). You should implement the Parcelable interface instead. There's a lot of cruft, but it's actually pretty simple, unless you have an extremely complex class. You can also implement Parcelable on the child classes, and then just use writeParcelableArray() for your lists of ingredients and steps.

I had the similar problem. After a long and tedious debugging, I realized that all the classes whose instances are used as member variables, are to be serialized. So, in your case, class Ingredient and class Step also need to be serialized, along with the class Recipe . I hope this helps :)

You don't need to override methods. Implementing interface should be enough. You haven't specified exactly what is not working, but complexity shouldn't be an issue as long as object you are trying to serialize has data which is serializable.

You don't need to implement anything. Just declaring the "implement Serializable" make the instances of the class serializable. as written in the documentation (http://developer.android.com/reference/java/io/Serializable.html) "Implementing this interface is enough to make most classes serializable."

It depends on the context of the application, I think. Parcelable is one option, but other apps implement a ContentProvider and pass a URL or an id in the Intent, which are then used to lookup the item in the ContentProvider.

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