Android RoboGuice2 Use Guide (2) First example Hello World

Source: Internet
Author: User

This example implements the features and examples of the Android Roboguice use Guide (2): The first example Hello World, the difference is this example uses RoboGuice2.0 to implement.

To download the new Roboguice Library, the Roboguice2.0 library consists of four libraries, as shown in the following illustration:

Libraries can be downloaded from the http://code.google.com/p/roboguice/.

2. Create a new Android project, such as Guicedemo, target platform Android1.5 above.

3. In general, you can add a libs directory under the project, copy two jar files to the Libs directory, and then through: Project > Properties > Java build Path > Libraries > Add JARs

Note: Starting with ADT17, the added jar file needs to be placed under the Libs subdirectory, see the issue of dalvikvm:unable to resolve superclass for upgrade to ADT 17

After adding references to the Guice and roboguice libraries, you can start writing the first example that uses Roboguice2.

Steps to use Roboguice2:

Roboguice2 does not contain roboapplication classes, so it is unnecessary and impossible to derive roboapplication subclasses. Here's a repeat of HelloWorld's layout and class descriptions.

1. In this simple example, it uses the layout definition as follows:

<?xml version= "1.0″encoding=" utf-8″?>

< LinearLayout xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

>

<textview

Android:id= "@+id/hello"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/hello"

/>

</linearlayout>

We have defined a textview, and its ID is hello.

Assuming that this application uses a igreetingservice, it has a method getgreeting () returns a string, as for Igreetingservice How to implement, Guidedemo does not need to care.

One of the core principles of the Dependency injection design pattern is: Separate behavior from Dependency resolution. It also says that the functionality to be implemented is decoupled from the services or other objects it relies on. For this example Guicedemo just know that it relies on igreetingservice services, as for Igreetingservice who realizes Guicedemo does not need to know.

Use @inject in Roboguice to represent this dependency.

public class Guicedemo extends roboactivity  {      

 @InjectView (R.id.hello) TextView Hellolabel;      
 @Inject Igreetingservice Greetingservce;      

 @Override public 
 void OnCreate (Bundle savedinstancestate) {      
 super.oncreate (savedinstancestate);      
 Setcontentview (r.layout.main);      
 Hellolabel.settext (Greetingservce.getgreetings ());      
 }      

An activity that uses roboguice needs to be derived from roboactivity (Roboactivity is a subclass of the activity).

Using the @inject annotation Greetingservce relies on the Igreetingservice service

Using @injectview to represent Hellolabel dependent on R.id.hello (XML)

There is no code in the code to create the Greetingservce object (such as New XXX ()) and the code to assign a value to Hellolabel. These values can be roboguice automatic creation and assignment injection (inject) into the variable.

To illustrate the problem, we add two implementations of Getgreetings to the code, one for HelloWorld and one for Hellochina:

Public 

class Hellochina implements igreetingservice{      

 @Override public 
 String getgreetings () {      
 return "Hello,china";      
 }      

}      

public class HelloWorld implements igreetingservice{      

 @Override public 
 String getgreetings () {return
 " Hello,world ";
 }

}

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.