[Android] how to set the default value after data is filled in for the spinner?

Source: Internet
Author: User

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.

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.