A small bug in Android alertcontroller?

Source: Internet
Author: User

I found this problem when I was debugging a program for an intern last Friday. He wanted to implement this function: when the user chooses to delete the program, the data list will pop up in the form of dialog, in addition, each data has a checkbox, which can be deleted and filtered after the user selects it. The specific interface is:

 

Public alertdialog. Builder setmultichoiceitems (cursor, string <br/> ischeckedcolumn, string labelcolumn, <br/> dialoginterface. onmultichoiceclicklistener listener)

 

The cursor parameter is the result returned from the database. ischeckedcolumn corresponds to the name of a column in the database. This parameter is an integer value, 0 indicates selected, 1 indicates selected, and labelcolumn indicates the fields displayed in the UI, listener is the registered click listener. Then implement the following function interfaces:

Public abstract void onclick (dialoginterface dialog, int which, Boolean <br/> ischecked)

In the dialog box created by the dialog parameter, which indicates the position of the table item you clicked in the list, and ischecked indicates whether the item is selected.

 

At that time, the query results can also be displayed, but the checkbox that you click does not change. After adding the log, it is found that the ischecked is already true, that is, the selected status should be displayed. After carefully reading the document, I found that there was no incorrect use. Finally, I could only view the implementation code in the framework. At first, I thought it was triggered by a click event, but the UI was not updated.

 

However, the Code implemented in listview is as follows (frameworks/base/CORE/Java/Android/widget/cursoradapter. Java ):Adapter = new cursoradapter (mcontext, mcursor, false) {<br/> private final int mlabelindex; <br/> private final int mischeckedindex; <br/>{< br/> final cursor = getcursor (); <br/> mlabelindex = cursor. getcolumnindexorthrow (mlabelcolumn); <br/> mischeckedindex = cursor. getcolumnindexorthrow (mischeckedcolumn); <br/>}</P> <p> @ override <br/> Public void BindView (view, context, <br/> cursor) {<br/> checkedtextview text = (checkedtextview) view. findviewbyid <br/> (R. id. text1); <br/> text. settext (cursor. getstring (mlabelindex); <br/> listview. setitemchecked (cursor. getposition (), <br/> cursor. getint (mischeckedindex) = 1); <br/>}</P> <p> @ override <br/> Public View newview (context, cursor, <br/> viewgroup parent) {<br/> return minflater. inflate (R. layout. select_dialog_multichoice, <br/> parent, false); <br/>}</P> <p >}; <br/>

 

This function will call the re-bind action when any event is triggered, in which listview. setitemchecked (cursor. getposition (), cursor. getint (mischeckedindex) = 1); is used to set the checkbox status. In the past, it was determined whether to select the checkbox based on the value of this column in the database mischeckedindex, so the first thing I think of is that I should update the database when the above onclick is called.

 

But the result does not happen as I imagined, and the UI is still not updated. However, I checked the database and found that the value has been modified. Will the query not be called again? So I see the third parameter of cursoradapter. Maybe you will understand the usage annotation of it:

 

Public cursoradapter (context, cursor C, Boolean autorequery)

Constructor

Parameters
Context The Context
C The cursor from which to get the data.
Autorequery If true the adapter will call requery () on the cursor whenever it changes so the most recent data is always displayed.

So we can change the third parameter to true. Android Developers are also human programmers, who make minor mistakes and hope to help those who may use this control :)

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.