Preface
You need to set the default options after adapting the data to the spinner. However, it is found that the setselection (INT position) option is selected when it is enabled, however, the displayed option is always the first one by default.Article1.
Article
1. Using spinner. setselection & finding the spinner doesn't show the selected item when closed?
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://www.cnblogs.com/over140/
Body
The problem is strange. In addition, it is also found that the first operation is selected by default after data adaptation, and the first operation is selected by default not to be executed immediately, but to be executed after a while, and triggers the onitemselectedlistener event. Go straight to the topic below:
OldCode:
Spinner. setadapter (adapter );
Spinner. setselection ( 2 );
New Code:
Spinner. setadapter (adapter );
Spinner. setselection ( 2 , True );
Here, we can see that setselection has two parameters for function overloading:
Setselection (INT position, Boolean animate)
English: Jump directly to a specific item in the adapter data.
Chinese: directly jump to the specified item in the data adapter.
The following are two functionsSource code:
/**
* Jump directly to a specific item in the adapter data.
*/
Public Void Setselection ( Int Position, Boolean Animate ){
// Animate only if requested position is already on screen somewhere
Boolean Shouldanimate = Animate && Mfirstposition <= Position &&
Position <= Mfirstposition + Getchildcount () - 1 ;< br> setselectionint (Position, shouldanimate );
}< br>
@ override
Public void setselection ( int position) {
setnextselectedpositionint (position);
requestlayout ();
invalidate ();
}
end
it looks special prepare a function for data adaptation (filling) it is a pity that the API documentation has not been translated into this document to set the default value. Otherwise, this detour will be skipped.