Technical Summary of the android project: Selecting the contact of the task in the project leads to the question of monitoring the android spinner.

Source: Internet
Author: User

In a web project, you can dynamically add Contacts and modify them, for example:

We can see from the above four figures that the contact function on the Web can have a maximum of seven lists. After each list is selected, it can be modified, as shown in Figure D. If you select a contact, the contact cannot be selected because there is no data.

Port to Android:

I want to implement this function on Android. One activity has a spinner. When I click one of the spinner items, a dialog box is displayed.
So I thought of the setonitemclicklistener method. The test found that there was an exception. I checked the source code and found that a spinner does not support item click events. calling this method will raise an exception. so I think this path may be different.
So I implemented this method: setonitemselectedlistener. below is my key code:
Taskcontactsspinner. setonitemselectedlistener (New onitemselectedlistener (){

Public void onitemselected (adapterview <?> Arg0, view arg1,
Int arg2, long arg3 ){
Alertdialog. Builder = new alertdialog. Builder (spinnertest. This );
Builder. settitle ("contacts ");
Builder. setitems (mitems, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
// The items selected in the pop-up window
New alertdialog. Builder (spinnertest. This)
. Setmessage ("the id you selected is" + which + "," + mitems [which])
. Show ();
}
}). Show ();
}

Public void onnothingselected (adapterview <?> Arg0 ){
// Todo auto-generated method stub

}
});
The statement in the onitemselected method in the above Code is a dialog box.
Test results show that when I enter the activity, the statement in onitemselected is automatically executed, and the effect I want is that only when I click one of the spinner, to bring up this dialog box. Many people on the Internet say that setselect (0, false) can be used to prevent the onitemselected statement from being executed at the beginning of the activity. On the surface, the result is correct, but in fact, this is just a false alarm, many people can find that after this is done, clicking the first entry on the spinner does not execute the action. Only when clicking the other entries in turn will the first one be reflected, however, if you click the same entry twice before and after, the second click is invalid. Why?
With this question, I checked the source code and found that the onitemselected method is triggered only when the content in the spinner changes. Although the data of the spinner is unchanged on the surface, however, when the ID of the entry we click changes, it is internally considered as a change in the spinner. The key code is as follows:
If (mdatachanged ){
Handledatachanged ();
}
In this case, you can explain the problem above. If you click the same entry twice, no action will happen. When you enter the activity at the beginning, the data in the spinner must also change. Therefore, the onitemselected statement is executed, so a dialog box is displayed at the beginning.
Later, I thought of adding a Boolean flag to determine whether to enter the activity for the first time and other methods, and found that the effect could not reach the expectation.

Related Article

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.