{"id":553,"date":"2011-05-31T12:37:10","date_gmt":"2011-05-31T04:37:10","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=553"},"modified":"2011-06-05T01:11:32","modified_gmt":"2011-06-04T17:11:32","slug":"%e5%b0%86%e6%95%b0%e6%8d%ae%e4%b8%8eadapterview%e7%bb%91%e5%ae%9a","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=553","title":{"rendered":"\u5c06\u6570\u636e\u4e0eAdapterView\u7ed1\u5b9a"},"content":{"rendered":"<p>AdapterView\u662fViewGroup\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5176\u5b50\u7c7bView\u7531\u4e00\u4e2aAdapter\u6765\u51b3\u5b9a\u4e0e\u4f55\u79cd\u7c7b\u578b\u7684\u6570\u636e\u76f8\u7ed1\u5b9a\u3002AdapterView\u5728\u9700\u8981\u5728\u5e03\u5c40\u4e2d\u663e\u793a\u50a8\u5b58\u7684\u6570\u636e\uff08\u4e0e\u8d44\u6e90\u5b57\u7b26\u4e32\u6216\u662f\u53ef\u7ed8\u5236\u5185\u5bb9\u76f8\u5bf9\uff09\u65f6\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<p>Gallery\u3001ListView\u548cSpinner\u662f\u53ef\u4ee5\u4ee5\u7279\u5b9a\u65b9\u5f0f\u7528\u6765\u4e0e\u7279\u6b8a\u7c7b\u578b\u6570\u636e\u7ed1\u5b9a\u663e\u793a\u7684AdapterView\u5b50\u7c7b\u7684\u51e0\u4e2a\u4f8b\u5b50\u3002<\/p>\n<p>AdapterView\u5bf9\u8c61\u6709\u4e24\u9879\u4e3b\u8981\u4efb\u52a1\uff1a<\/p>\n<ul>\n<li>\u7528\u6570\u636e\u586b\u5145\u5e03\u5c40<\/li>\n<li>\u5904\u7406\u7528\u6237\u9009\u62e9<\/li>\n<\/ul>\n<h2>\u7528\u6570\u636e\u586b\u5145\u5e03\u5c40<\/h2>\n<p>\u901a\u8fc7\u628aAdapterView\u7c7b\u4e0e\u4e00\u4e2aAdapter\u7ed1\u5b9a\u53ef\u4ee5\u5c06\u6570\u636e\u63d2\u5165\u5e03\u5c40\u4e4b\u4e2d\uff0cAdapter\u4f1a\u4ece\u5916\u90e8\u8d44\u6e90\u83b7\u53d6\u6570\u636e\uff08\u6bd4\u5982\u5728\u4ee3\u7801\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u5217\u8868\u6216\u662f\u6765\u81ea\u8bbe\u5907\u7684\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\uff09\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u8303\u4f8b\u4ee3\u7801\u7684\u529f\u80fd\u662f\uff1a<\/p>\n<ol>\n<li>\u501f\u52a9\u4e8e\u4e00\u4e2a\u5df2\u6709\u7684View\u521b\u5efa\u4e00\u4e2aSpinner\u5e76\u5c06\u5176\u7ed1\u5b9a\u81f3\u4e00\u4e2a\u65b0\u7684ArrarAdapter\u6765\u4ece\u672c\u5730\u8d44\u6e90\u4e2d\u8bfb\u53d6\u4e00\u5217\u8272\u5f69\u3002<\/li>\n<li>\u5728\u4e00\u4e2aView\u4e2d\u521b\u5efa\u53e6\u4e00\u4e2aSpinner\u5e76\u5c06\u5176\u7ed1\u5b9a\u81f3\u4e00\u4e2a\u65b0\u7684SimpleCursorAdapter\u6765\u4ece\u8bbe\u5907\u901a\u8baf\u5f55\u4e2d\u8bfb\u53d6\u8054\u7cfb\u4eba\u59d3\u540d\uff08\u53c2\u89c1Contacts.People\uff09\u3002<\/li>\n<\/ol>\n<pre><span style=\"color: #993300;\">\/\/ Get a Spinner and bind it to an ArrayAdapter that <\/span>\r\n<span style=\"color: #993300;\">\/\/ references a String array.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Spinner <\/span>s1 = (<span style=\"color: #800080;\">Spinner<\/span>) findViewById(R.id.spinner1);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">ArrayAdapter <\/span>adapter = <span style=\"color: #800080;\">ArrayAdapter<\/span>.createFromResource(<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 this<\/span>, R.array.colors, android.R.layout.simple_spinner_item);<\/span>\r\n<span style=\"color: #000000;\">adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);<\/span>\r\n<span style=\"color: #000000;\">s1.setAdapter(adapter);<\/span>\r\n\r\n<span style=\"color: #993300;\">\/\/ Load a Spinner and bind it to a data query.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">private static <\/span><span style=\"color: #800080;\">String<\/span>[] PROJECTION = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">String<\/span>[] {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 People<\/span>._ID, <span style=\"color: #800080;\">People<\/span>.NAME<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 };<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Spinner <\/span>s2 = (<span style=\"color: #800080;\">Spinner<\/span>) findViewById(R.id.spinner2);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Cursor <\/span>cur = managedQuery(<span style=\"color: #800080;\">People<\/span>.CONTENT_URI, PROJECTION, <span style=\"color: #000080;\">null<\/span>, <span style=\"color: #000080;\">null<\/span>);<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">SimpleCursorAdapter <\/span>adapter2 = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">SimpleCursorAdapter<\/span>(<span style=\"color: #000080;\">this<\/span>,<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 android.R.layout.simple_spinner_item, <span style=\"color: #993300;\">\/\/ Use a template<\/span><\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ that displays a<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ text view<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 cur, \/\/ Give the cursor to the list adapter<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 new <\/span><span style=\"color: #800080;\">String<\/span>[] {<span style=\"color: #800080;\">People<\/span>.NAME}, <span style=\"color: #993300;\">\/\/ Map the NAME column in the<\/span><\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ people database to...<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 new int<\/span>[] {android.R.id.text1}); <span style=\"color: #993300;\">\/\/ The \"text1\" view defined in<\/span><\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ the XML template<\/span>\r\n\r\n<span style=\"color: #000000;\">adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);<\/span>\r\n<span style=\"color: #000000;\">s2.setAdapter(adapter2);<\/span><\/pre>\n<p>\u6ce8\u610f\u5fc5\u987b\u8ba9People._ID\u680f\u901a\u8fc7CursorAdapter\u6295\u5f71\uff0c\u4e0d\u7136\u4f1a\u6536\u5230\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n<p>\u5982\u679c\u5728\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u5185\u6539\u53d8\u4e86Adapter\u6240\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u90a3\u5c31\u8be5\u8c03\u7528notifyDataSetChanged()\u3002\u8fd9\u5c06\u544a\u8bc9\u88ab\u7ed1\u5b9a\u7684View\u6570\u636e\u5df2\u88ab\u6539\u53d8\u4ee5\u5237\u65b0\u663e\u793a\u3002<\/p>\n<h2>\u5904\u7406\u7528\u6237\u9009\u62e9<\/h2>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5c06\u7c7b\u7684AdapterView.OnItemClickListener\u6210\u5458\u4e0e\u4e00\u4e2a\u76d1\u542c\u5668\uff08listener\uff09\u76f8\u7ed3\u5408\u5e76\u6355\u83b7\u9009\u62e9\u52a8\u4f5c\u6765\u5904\u7406\u7528\u6237\u9009\u62e9\u3002<\/p>\n<pre><span style=\"color: #800000;\">\/\/ Create a message handling object as an anonymous class.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">private <\/span><span style=\"color: #800080;\">OnItemClickListener <\/span>mMessageClickedHandler = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">OnItemClickListener<\/span>() {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void <\/span>onItemClick(<span style=\"color: #800080;\">AdapterView <\/span>parent, <span style=\"color: #800080;\">View <\/span>v, <span style=\"color: #000080;\">int <\/span>position, <span style=\"color: #000080;\">long <\/span>id)<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 {<\/span>\r\n<span style=\"color: #800000;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Display a messagebox.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 Toast<\/span>.makeText(mContext,<span style=\"color: #008000;\">\"You've got an event\"<\/span>,<span style=\"color: #800080;\">Toast<\/span>.LENGTH_SHORT).show();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">};<\/span>\r\n\r\n<span style=\"color: #800000;\">\/\/ Now hook into our object and set its onItemClickListener member<\/span>\r\n<span style=\"color: #800000;\">\/\/ to our class handler object.<\/span>\r\n<span style=\"color: #000000;\">mHistoryView = (<span style=\"color: #800080;\">ListView<\/span>)findViewById(R.id.history);<\/span>\r\n<span style=\"color: #000000;\">mHistoryView.setOnItemClickListener(mMessageClickedHandler); <\/span><\/pre>\n<p>\u5173\u4e8e\u5982\u4f55\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684AdapterView\u7684\u66f4\u591a\u5185\u5bb9\uff0c\u8bf7\u9605\u8bfb\u4e0b\u9762\u7684\u6559\u7a0b\uff1aHello Spinner\u3001Hello ListView\u4ee5\u53caHello GridView\u3002<\/p>\n<p>\u672c\u4f5c\u54c1\u91c7\u7528<a title=\"\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae\" rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/\" target=\"_blank\">\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae<\/a>\u8fdb\u884c\u8bb8\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AdapterView\u662fViewGroup\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5176\u5b50\u7c7bView\u7531\u4e00\u4e2aAdapter\u6765\u51b3\u5b9a\u4e0e\u4f55\u79cd\u7c7b\u578b\u7684\u6570\u636e &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=553\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5c06\u6570\u636e\u4e0eAdapterView\u7ed1\u5b9a\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[64],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":5,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":557,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/557"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}