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
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.