Passing object (with multidimensional array) from intent to another - android

I have a simplest object, and for this I use parcelable. But this object is more complex, have multidimensional array and I really don't know how to write the parcelable methods:
public class PointSystem
{
private int point;
private boolean [] vec1;
private HashSet <Integer> hs1;
private int [][] vecMap;
}
I removed the other istance variable of the same type and the methods so the code is more readable.
I tried with serializable but I don't know how to cast from the other intent the serializable that I get to array [][].
How could I make this object parcelable? Or there're other way to pass this object to another intent?

First off, you say you try to cast the serializable you get to array[][], are you trying to pass all of the members of this object as separate extras? Why not serialize the entire PointSystem object and pass it as an extra. Then, when you try to receive it:
PointSystem p = (PointSystem) getIntent().getSerializableExtra("Extra_Name");
int[][] vecMap = p.vecMap;
A good example of using Parcelable can be found within this answer

Related

Serializable in Intent/Bundle not working

I moved the Model to an external Dependency so I can reuse it on Server and other Parts. But now I'm having the Problem that when I serialize to Intents Extra and then deserialize in BroadcastReceiver the deserialized Object is not null but it's Properties have the default Values like null (for the Strings) or 0 (for int or long).
This is excerpt from the Models Class:
public class Measurement extends Event implements Serializable {
private static final long serialVersionUID = 8246754793603601250L;
/* Some other Stuff */
}
And this from Serialization:
List<Measurement> measurements = new ArrayList<Measurement>();
/* measurements get filled */
extras.putSerializable(Intents.EXTRA_MEASUREMENTS, (Serializable) measurements);
And this from Deserialization:
List<Measurement> measurements = (List<Measurement>) intent.getSerializableExtra(Intents.EXTRA_MEASUREMENTS);
All members of Measurement and members of its members (recursion) would be Serializable
Found it. Problem was that the underlying Class Event didn't implement Serializable. Looks like this got lost during Refactoring and moving to an external Dependency.
Thanks to everybody.

Putting serializable extra object to intent changes the object

I am trying to pass an object from an activity to another activity. Here is what i do:
MyApplication.db= dbToOpen;
Intent i = new Intent(mContext, OpenDbActivity.class);
i.putExtra("PARENT_GROUP", dbToOpen.root);
mContext.startActivity(i);
Here, MyApplication is the class that extends application, and db object is a static object. My extra object dbToOpen.root is an object of the class DBGroupv1.
Then i get this extra in onCreate method of OpenDbActivity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opendb);
db = MyApplication.db;
groupToOpen = (DBGroupv1) getIntent().getSerializableExtra("PARENT_GROUP");
}
Then i try this boolean expression:
MyApplication.db.root == groupToOpen
and it returns false. When i look at the objects dbToOpen.root and groupToOpen, every single value of the variables inside those objects are the same. But they are are different objects. Why is this happening? Is it because of casting, or does Intent.putextra() method passes a copy of an object, not a reference? If that is the case how can i pass the object as a reference?(Except using static variables)
Thanks
You should use the .equals()-method to compare instances of objects. if you use == you will only get true if the two objects are exactly the same reference. Since the instance in your intent is newly created when deserialized from the bundle, it is no longer a reference to the same instance (although the two objects contains the same data).
So, instead of
MyApplication.db.root == groupToOpen //bad
use
MyApplication.db.root.equals(groupToOpen) //good
Also make sure that if you made the root-object, you implement the equals method properly, so it takes all appropriate variables into consideration.
You can read a bit more here: What is the difference between == vs equals() in Java?

Intent putextra serializable object array

my problem is putextra method with serializable object array. I mean if i try bottom code it throws Caused by: java.io.NotSerializableException:
Here 's the code :
class Example implements Serializable
{
private int ID; // It has getters and setters and also other variables.
}
Intent inte=new Intent(this,OTHERCLASS.class);
Example[] examples=new Example[]; // It's just an example.
Bundle bundle = new Bundle();
bundle.putSerializable("Details", examples);
inte.putExtras(bundle);
startActivity(inte);
Thanks.
this is because you can't serialize a inner class without making its parent class serializable. Which in your case is your Activity. So simply create a new java file for your Example class
Although your class is serializable, an Array of items with your class (Example[]) is not serializable. Edit: Thanks #gomino for pointing out that this was wrong. I just assumed this was the reason for the problem without actually thinking about it.
Also, it would be more efficient to use a Parcelable instead. You can find a tutorial here.

Bundle array of arrays is not Working - Android

Im tryin to bundle an Array of Arrays but is not working. Heres a snipped of code for better understanding:
Declaring and Initializing the variable
Inversor[][] reg_equipment= new Inversor[7][5];
for(int i=0; i<7; i++)
{
for(int j=0;j<5;j++)
{
reg_equipment[i][j]= new Inversor();
}
}
//....
Putting the variable in the bundle
bundle.putSerializable("reg_equipment", reg_equipment);
Intent myIntent =new Intent(RegisterEquipmentInversor.this,RegisterEquipmentMain.class);
myIntent.putExtras(bundle);
startActivity(myIntent);
At this point, the reg_equipment is filled with Inversors [Inversor[0],Inversor[1]....,Inversor[6]] and inside those There are more Inversors.
But when I go "get" the bundle in the other class
reg_equipment = (Inversor[][]) extras.getSerializable("reg_equipment");
This is whats inside de reg_equipment - [Object[0],Object[1],...,[Object[6]] and inside those Objects there are Inversors. Why does this happens ? How can I fix it ?
The class Inversor implements Serializable
Thanks
You should try to create a Serializable class that has only one property, which should be your array of Inversor arrays and put that object in your intent.
something like
public class InversorArrays implements Serializable {
public final static int serialVersionUID = //let eclipse generate your uid
public Inversor[][] myArray = null;
public InversorArrays (Inversor[][] _myArray){
this.myArray = _myArray;
}
}
and then, in your activity, create an instance of InversorArrays an pass it to the intent
Of course, Inversor and its properties should be serializable too.
This workaround sometimes saved me a lot of time and problems with typecasting and conversion problems
I am not sure, but have you made the investor class serializable? I think if we could get a basic view of the investor class, that may lead to some light.
I would say start off by making Investor serializable. http://www.tutorialspoint.com/java/java_serialization.htm

Android: Intent with parameters

To pass parameters from one activity to another is used the method "intent.putExtra()"
Does this method only allows to add primitive data or I can add a parameter that is a java bean?
if you can not, how I can send a java bean from one activity to another?
Thanks!!
Look at the API entry for Intent. You've got a load of possible data types you can enter, not the least of which is Parcebles, Bundles, and Serializable. If you really want simple object marshalling I would convert your beans to JSON and put it as a String, then convert it back to a POJO on the receiving end.
You can send objects if they implements serializable.
//At your entity Object:
public class Objeto implements Serializable{
}
//At the sender Activity:
//create an instance of the object
Objeto object = new Objeto();
//creates an intent from the current activity to the destiny activity with the data to be transferred.
Intent proximo = new Intent(this,TelaDestino.class);
//transfers the object as a bundle to the next activity
proximo.putExtra("OBJETO",objeto);
startActivity(proximo);
//At the destine activity:
private Objeto objeto;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
objeto = (Objeto)getIntent().getSerializableExtra("OBJETO");
}
//You can use that and be happy :D
If your object is is Serializable you can add it with putExtra like this.
i.putExtra(String key, Serializable value);
Another way to share data between activities is extend the application class.
My answer explains how to use it.
getApplicaiton return which object among applicaitons

Categories

Resources