{"id":959,"date":"2023-03-11T19:37:24","date_gmt":"2023-03-11T11:37:24","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=959"},"modified":"2023-04-29T13:29:57","modified_gmt":"2023-04-29T05:29:57","slug":"android-use-parcelable-to-serialize-complex-data-structures","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-use-parcelable-to-serialize-complex-data-structures\/","title":{"rendered":"Android\u4f7f\u7528Parcelable\u5e8f\u5217\u5316\u590d\u6742\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<p>Android\u5e8f\u5217\u5316\u65b9\u6848Parcelable\uff0c\u76f8\u6bd4Serializable\u66f4\u5feb\uff0c\u6548\u7387\u66f4\u9ad8\uff0c\u5360\u7528\u5185\u5b58\u66f4\u5c11\u3002<\/p>\n<h2>Boolean\u7c7b\u578b<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/write\ndest.writeByte((byte) (isDefault ? 1: 0));\n\n\/\/read\nisDefault = source.readByte() != 0;<\/code><\/pre>\n<h2>List\u7c7b\u578b<\/h2>\n<pre><code class=\"language-java\">\/\/write\ndest.writeList(list);\n\n\/\/read\nlist = source.readArrayList(String.class.getClassLoader());\nlist = source.readArrayList(Integer.class.getClassLoader());\nlist = source.readArrayList(MyEntity.class.getClassLoader());<\/code><\/pre>\n<h2>Map\u7c7b\u578b<\/h2>\n<pre><code class=\"language-java\">\/\/write\ndest.writeMap(map);\n\n\/\/read\nmap = source.readHashMap(HashMap.class.getClassLoader());<\/code><\/pre>\n<h2>Array\u6570\u7ec4<\/h2>\n<pre><code class=\"language-java\">\/\/write\nif (array != null) {\n    dest.writeInt(array.length);\n} else {\n    dest.writeInt(0);\n}\ndest.writeStringArray(array);\n\n\/\/read\nint length = source.readInt();\narray = new String[length];\nsource.readStringArray(array);<\/code><\/pre>\n<h2>\u5b9e\u4f53\u7c7b<\/h2>\n<pre><code class=\"language-java\">\/\/\u5982\u679c\u5b9e\u4f53\u7c7b\u5b9e\u73b0Serializable\u63a5\u53e3\n\/\/write\ndest.writeSerializable(entity);\n\/\/read\nsource.readSerializable();\n\n\/\/\u5982\u679c\u5b9e\u4f53\u7c7b\u5b9e\u73b0Parcelable\u63a5\u53e3\n\/\/write\ndest.writeParcelable(entity, flags);\n\/\/read\nsource.readParcelable(MyEntity.class.getClassLoader());<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u5e8f\u5217\u5316\u65b9\u6848Parcelable\uff0c\u76f8\u6bd4Serializable\u66f4\u5feb\uff0c\u6548\u7387\u66f4\u9ad8\uff0c\u5360\u7528\u5185\u5b58\u66f4\u5c11\u3002 Bo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-959","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=959"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}