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.