Converting string to object

Is it possible to convert a string that was created by Object.ToString() back to the original object i.e:

            public boolean dispatchKeyEvent (KeyEvent event) {     
                appendLog(event.toString());
                return super.dispatchKeyEvent(event);      
            }

appent log will log every keyevent as a string in a new line. After logging i would like to run the logged keyevents

Is it possible to convert the string logged keyevents to a KeyEvent object?

Answers


Only if the toString() contains all the necessary event information. If it's just a hash code, of course not. If the relevant info is available, you'd need to parse it, and use one of the KeyEvent constructors, and hope there's nothing internally that isn't reflected in the toString. Seems sketchy to me.

If you need to store it, it's Parcelable, that might help.

You still have the event in the snippet you show, without an actual usecase it's hard to know what you're trying to do.


In general, no. The String won't contain enough information to reconstruct the original object. Of course, if you do encode enough information in the String returned by toString(), you may reconstruct the object - for instance, you can serialize an object to an XML - a String, and from that XML rebuild the object that originated it. Take a look at JAXB.


Need Your Help

Echo category name once per id

php mysql

I have a query SELECT id, title, imagename FROM cat JOIN images ON id=id

Query results that have less than X characters in MySQL

mysql select character

Some submitters have submitted location strings that are too short.

Using RSpec to test SuckerPunch::Job

rspec sucker-punch

I've defined the following SuckerPunch Job:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.