{"id":349,"date":"2023-02-25T07:05:21","date_gmt":"2023-02-24T23:05:21","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=349"},"modified":"2023-02-25T07:05:21","modified_gmt":"2023-02-24T23:05:21","slug":"android-listview-addscrapview-arrayindexoutofboundsexception","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-listview-addscrapview-arrayindexoutofboundsexception\/","title":{"rendered":"Android ListView addScrapView ArrayIndexOutOfBoundsException"},"content":{"rendered":"<p>\u4f7f\u7528ListView\u81ea\u5b9a\u4e49Adapter\u65f6\u51fa\u73b0ArrayIndexOutOfBoundsException\u9519\u8bef\uff1a<\/p>\n<pre><code>java.lang.ArrayIndexOutOfBoundsException: length=2; index=2\n    at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:5996)\n    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4554)\n    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3874)\n    at android.os.Handler.handleCallback(Handler.java:605)\n    at android.os.Handler.dispatchMessage(Handler.java:92)<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>CustomAdapter \u7684 getViewTypeCount() \u65b9\u6cd5\u8fd4\u56de\u503c\u4e3a2\uff0c\u8868\u793aListView\u4e2d\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u89c6\u56fe\uff0cgetItemViewType() \u7684\u8fd4\u56de\u503c\u5206\u522b\u4e3a1\u62162\uff0c\u53d6\u51b3\u4e8eitem\u5728ListView\u4e2d\u7684\u4f4d\u7f6e\u3002getItemViewType() \u7684\u8fd4\u56de\u503c\u5e94\u8be5\u662f\u4ece\u4e0b\u68070\u5f00\u59cb\u7684\uff0c\u6240\u4ee5\u5e94\u8be5\u8fd4\u56de0\u6216\u80051\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">static final int COMMON_ITEM = 0;\nstatic final int CLEAR_ITEM = 1;\n\n@Override\npublic int getViewTypeCount() {\n    return 2;\n}\n\n@Override\npublic int getItemViewType(int position) {\n    if (position != historyKey.length) {\n        return COMMON_ITEM;\n    } else {\n        return CLEAR_ITEM;\n    }\n}<\/code><\/pre>\n<p>\u5c06getItemViewType() \u7684\u8fd4\u56de\u503c\u6539\u4e3a 0\u6216\u80051\uff0c\u8fd0\u884c\u4e0d\u62a5\u9519\u4e86\uff0c\u95ee\u9898\u89e3\u51b3\u5566\uff01<\/p>\n<blockquote>\n<p>\u6700\u540e\u603b\u7ed3\u4e00\u70b9\u662f\uff1aAdapter\u7684getViewTypeCount \u65b9\u6cd5\u548cgetItemViewType\u65b9\u6cd5\u8fd4\u56de\u503c\u4e4b\u95f4\u5b58\u5728\u4e00\u5b9a\u7684\u5173\u7cfb\u3002\u5982\u679cgetViewTypeCount \u8fd4\u56de\u503c\u4e3a 3\uff0c\u90a3\u4e48getItemViewType\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u5e94\u8be5\u662f 0\u30011\u30012\uff0c\u4e0d\u80fd\u8d85\u8fc72\uff0c\u5426\u5219\u4f1a\u51fa\u73b0ArrayIndexOutOfBoundsException\u5f02\u5e38\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528ListView\u81ea\u5b9a\u4e49Adapter\u65f6\u51fa\u73b0ArrayIndexOutOfBoundsException\u9519\u8bef [&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":[122],"class_list":["post-349","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-listview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/349","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=349"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/349\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}