File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Passing my object to one to other activity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Passing my object to one to other activity" Watch "Passing my object to one to other activity" New topic
Author

Passing my object to one to other activity

vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Hi all

I want to pass my object to one activity to other.
eg. I have class myClass which holds sting array, integer, Vector, Hash.
Now I have activity A which set the myClass which stored through json parsing and that object pass to Activity B then B to C.
So how can I pass the object of myClass from activity A to B?
putExtra() working for String, integer only.
I dont have only the myClass object to pass , I may have to pass other object also with it.

Please help.

Thanks,
vina
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

you can use putExtra(Serializable..) and getSerializableExtra() methods to pass and retrieve objects of your class type; you will have to mark your class Serializable and make sure that all your member variables are serializable too...


[List of FAQs] | [Android FAQ] | [Samuh Varta]
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Thanks Monu.
First it was giving me error then after restarting it's working fine.
You are really helping a lot.
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
How can I serialize the Hashtable to pass it?
I can pass myClass but not able to pass Hashtable which is in other class.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I can pass myClass but not able to pass Hashtable which is in other class

I am not sure I understand what you are saying.

Hashtable IS Serializable.
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
I am also cofused.
My code is



It will get the object of myClass but giving error at hashtable
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
i am not sure whether i can understand your question

i will give this however

Not all types are serializable.....

check with the api specification whether the java class you have used can be serializable....

If your class uses the file object ,then even though the user defined class is serialized and passed the file object will not pass .....

so make sure the hashtable is serializable

i hope this answer is okay


To err is human,
To forgive is not company policy
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Ok
Let me clarify

I have to pass Hashtable, and other one is myClass which contains Vectors (myClass is Serializable no file class simple vector in myClass).

Now when I passing the object of hashtable and myClass obj Activity A to B, then it will show me the value of myClass and its vectors but giving me error for hashtable.

And How can I know Hashtable is Serializable?

Thanks
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
First to know whether a particular java class is serialized(predefined java class like collections,swing and others) type the class name + api in google.It will take you to the java api page where it will have a name called All implemented Interfaces :see whether it has serializable in it .

Example
For hashtable use this link:http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html

are you passing the intent object from one system to another..if yes it must also be serialized....

Actually what is activity A and activity B

For my college project i spent nearly three months on the serialization problem alone

If your myclass is working then the hashtable must also work

I have the following questions
1)what is activity A and B
2) check whether you have overloaded get methods in bundle......
3)you have not given any information about the bundle class and intent class

vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Thanks for the reply

OK even I am not able to understand your que.
Let me explain again.
Activity Class A contains EditText, Spinner and Button. On Click event of Button it parse json and put the values into the Vectors this will done in myClass which is serialized.
After that as per records I am putting values in the Hashtable which is related to Vectors.
So i have to pass both to the Activity Class B which will display them.

I am not overloading get method.
I am not using Bundle and Intent class more then the given code.

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I tried sending Hashtable using put(Serializable) and getSerializableExtra(..); I am getting ClassCastExceptions..
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Hi

It is giving me error as : Error in B(304): java.lang.ClassCastException: java.util.HashMap


Anything I am missing.
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
So how do i do this.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Okay, I am not sure of why this is happening but it seems that if you cast your object to a HashMap instead of HashTable and then retrieve the value using a key, the code actually works!

vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Thanks Monu,

I have tried HashMap and it works fine.
But in next Activity I have to use Vector then it will again create problem.

If you can find the solution please reply.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Reminder: just because a container class is serializable doesn't mean that the container object is serializable. If you store a non-serializable object in a HashMap or Vector, the entire HashMap or Vector becomes effectively non-serializable.

This won't result in compile-time errors. The compiler cannot usually deduce such things.


Customer surveys are for companies who didn't pay proper attention to begin with.
vina parmar
Greenhorn

Joined: Nov 09, 2009
Posts: 22
Thanks Tim

It will help me a lot.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Tim Holloway wrote:Reminder: just because a container class is serializable doesn't mean that the container object is serializable. If you store a non-serializable object in a HashMap or Vector, the entire HashMap or Vector becomes effectively non-serializable.

This won't result in compile-time errors. The compiler cannot usually deduce such things.

Good Point.

The strange thing for me however is the change in type information. I created a Hashtable(IS-A Serializable) and inserted just one String(IS-A Serializable) object into it. I passed this Hashtable as an intent extra. On the receiving end, I did a getSerializableExtra() to get Serializable which I was casting to Hashtable. JVM says the cast is Illegal. However, if I cast the object to a HashMap, I can read the values stored in the data structure.

I am wondering how a Hashtable type is converted to a HashMap type??!!
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
hashtable is a subclass of java.util.dictionary

as of java1.2 hashtable implements map interface......

I think that may be the reason that hashmap accepts a hashtable object

maybe while reading it again it may have found only the map properties.....

I am given reasons vaguely.....but it may be the starting point of understanding the casting.

 
Consider Paul's rocket mass heater.
 
subject: Passing my object to one to other activity