Android RoboGuice User Guide (12): How to bind a generic type

Source: Internet
Author: User

To inject a parameterized type, such as List <String>:

[Java] class Example {
@ Inject
Void setList (List <String> list ){
...
}
}
Class Example {
@ Inject
Void setList (List <String> list ){
...
}
}

You can use TypeLiteral to create this binding. TypeLiteral is a special type that can be used to represent parameterized types.

[Java] @ Override public void configure (){
Bind (new TypeLiteral <List <String >> (){})
. ToInstance (new ArrayList <String> ());}
@ Override public void configure (){
Bind (new TypeLiteral <List <String >> (){})
. ToInstance (new ArrayList <String> ());}

Or use the @ Provides method:

[Java] @ Provides List <String> providesListOfString (){
Return new ArrayList <String> ();
}
@ Provides List <String> providesListOfString (){
Return new ArrayList <String> ();
}

So far, I have introduced the usage of Google Guice, which is also applicable to Java SE and Java EE platforms. For more information, see the English documentation, next we will introduce the Dependency Injection (Roboguice) usage related to the Android platform.

 

Excerpted from the mobile app

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.