Use checkbox in Android listview

Source: Internet
Author: User
<span id="Label3"></p><p><p>Recently busy, I do not know what is busy, I intend to write half of the blog, have not finished writing, today first pull a pull on the project encountered a problem, on the one hand to prevent later encounter this problem forget how to solve, on the other hand hope can provide to meet the same problem friend a train of Thought.<br>To get started, use a checkbox in the ListView item, and when you use the normal ListView usage, you will encounter the following problem:<br>1.checkbox can be clicked, but the ListView item cannot be clicked<br>2. Unable to get what line the checkbox was clicked on<br>The first issue relates to the distribution of events, the next one (i will follow my understanding, easy to understand to describe), the second question my understanding is GetView cannot redraw the reason, here can not get the value of position, in order to solve my problem, I need to change a way of thinking, that is, I introduced the method today : A selection event that masks the checkbox, which means that the checkbox cannot respond to the selected event, creating an illusion when the user clicks on the ListView item, using the Code to control the checkbox</p></p><p><p>well, That's about it, because the code is written directly into the project, do not write the demo, directly paste the main code, for reference only, if you have a better way to communicate with each other</p></p><p><p><strong>1, Set checkbox cannot respond to click events</strong></p></p> <ol class="dp-xml" start="1"> <ol class="dp-xml" start="1"> <li class="alt"><span class="attribute">android:clickable=<span class="attribute-value">"false"</span></span></li> <li><span class="attribute">android:focusable=<span class="attribute-value">"false"</span> </span></li> <li class="alt"><span class="attribute">android:focusableintouchmode=<span class="attribute-value">"false"</span> </span></li> </ol> </ol><p><p><br><strong>2. Declare a value in the adapter to control the checkbox selection</strong></p></p> <ol> <ol> <li>private static hashmap<integer,boolean> isSelected;</li> </ol> </ol><p><p></p></p> <ol> <ol> <li>public static hashmap<integer,boolean> getisselected () {</li> <li>Return isSelected;</li> <li>}</li> <li></li> <li>public static void setisselected (hashmap<integer,boolean> IsSelected) {</li> <li>mydocshowadapter.isselected = isSelected;</li> <li>}</li> </ol> </ol><p><p></p></p> <ol> <ol> <li>isSelected = new hashmap<integer, boolean> ();</li> <li>For (int i=0; i<list.size (); I++) {</li> <li>getisselected (). put (i,false);</li> <li>}</li> </ol> </ol><p><p><strong>3. Assign a checkbox to the check according to the Item's click situation</strong></p></p> <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt"><li class="alt">Mlistview.setonitemclicklistener (<span class="keyword">new Onitemclicklistener () {</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="annotation">@Override</span></li></li> <li><li><span class="keyword">public <span class="keyword">void Onitemclick (adapterview<?> parent, view view,</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">int position, <span class="keyword">long Id) {</span> </span></li></li> <li><li>Viewholder holder = (viewholder) View.gettag ();</li></li> <li class="alt"><li class="alt">Holder.mCheckBox.toggle ();</li></li> <li><li>mydocshowadapter.getisselected (). put (position, holder.mCheckBox.isChecked ());</li></li> <li class="alt"><li class="alt">}</li></li> <li><li>});</li></li> </ol> </ol><p><p>Use checkbox in Android listview</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.