简体   繁体   English

保存类对象而不进行序列化

[英]Saving a class object without serialization

i have a class named MyClass, with a few attributes String name, String place, int num.... 我有一个名为MyClass的类,具有一些属性字符串名称,字符串位置,整数…。
I read android developer site but i dont realy understand how it works. 我读了android开发人员网站,但我真的不明白它是如何工作的。
So i want a method which save these into a file or somewhere, and when i want to read it, from another activity or anywhere i can put it to an array or something so i get g.Name, g.place and the other attributes. 所以我想要一种将这些保存到文件或某个地方的方法,当我想从另一个活动或任何地方读取它时,我可以将其放入数组或其他东西,这样我就可以得到g.Name,g.place和其他属性。

And i also want to be able edit and delete these. 而且我也希望能够编辑和删除这些。

If anyone knows a good solution please write. 如果有人知道一个好的解决方案,请写信。 And sorry for my english. 对不起,我的英语。

SharedPreferences is one option that is easy to use. SharedPreferences是一种易于使用的选项。 There is a good guide for getting started with them here: http://developer.android.com/guide/topics/data/data-storage.html#pref 这里有一个很好的入门指南: http : //developer.android.com/guide/topics/data/data-storage.html#pref

You could use JacksonJSON or gson from google. 您可以使用Google的JacksonJSON或gson。 That way you can write objects to a file and read it again. 这样,您可以将对象写入文件并再次读取。

You can use XStream which is able to serialize any object to XML (of course with some assumptions). 您可以使用XStream,它能够将任何对象序列化为XML(当然有一些假设)。 It's slower than serialization but in your case it should work fine. 它比序列化要慢,但是在您的情况下应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM