Skip to content

Commit 48514b4

Browse files
committed
add syntax highlighting to android post
1 parent 9138276 commit 48514b4

File tree

1 file changed

+95
-75
lines changed

1 file changed

+95
-75
lines changed

_posts/2012-02-22-tour-d-horizon-des-frameworks-java-pour-android.markdown

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,19 @@ Comme son nom l'indique, ce framework apporte un bon nombre d'annotations
5151
qui nous permettent d'éliminer beaucoup de code boilerplate. Un exemple
5252
valant mieux qu'un long discours :
5353

54-
@EActivity(R.layout.mon_activite) // content view => R.layout.mon_activite
55-
public class MyActivity extends Activity {
56-
@InjectView // Injection de R.id.titre
57-
TextView titre;
58-
59-
@DrawableRes(R.drawable.logo)
60-
Drawable logo
61-
62-
@SystemService
63-
SearchManager searchManager;
64-
}
54+
{% highlight java %}
55+
@EActivity(R.layout.mon_activite) // content view => R.layout.mon_activite
56+
public class MyActivity extends Activity {
57+
@InjectView // Injection de R.id.titre
58+
TextView titre;
59+
60+
@DrawableRes(R.drawable.logo)
61+
Drawable logo
62+
63+
@SystemService
64+
SearchManager searchManager;
65+
}
66+
{% endhighlight %}
6567

6668
Le framework fonctionne par génération de code à la compilation ([JAPT](http://docs.oracle.com/javase/6/docs/technotes/guides/apt/index.html)) en
6769
créant des classes suffixées d'un _. Une activity MyActivity devient donc
@@ -90,19 +92,21 @@ Projet très jeune et peu documenté, pas du tout prêt à être utilisé.
9092

9193
ORM basé sur des annotations :
9294

93-
@DatabaseTable(tableName = "accounts")
94-
public class Account {
95-
@DatabaseField(id = true)
96-
private String name;
97-
98-
@DatabaseField(canBeNull = false)
99-
private String password;
100-
...
101-
Account() {
102-
// all persisted classes must define a no-arg constructor with at least package visibility
103-
}
104-
...
105-
}
95+
{% highlight java %}
96+
@DatabaseTable(tableName = "accounts")
97+
public class Account {
98+
@DatabaseField(id = true)
99+
private String name;
100+
101+
@DatabaseField(canBeNull = false)
102+
private String password;
103+
...
104+
Account() {
105+
// all persisted classes must define a no-arg constructor with at least package visibility
106+
}
107+
...
108+
}
109+
{% endhighlight %}
106110

107111
Il s'intègre bien dans les applications Android (l'API pour Android est [](http://ormlite.com/javadoc/ormlite-android/)).
108112
L'intégration avec RoboGuice est possible et on obtient alors une stack qui
@@ -148,36 +152,40 @@ Le model gère les données et les hanlders (on peut voir la déclaration de la
148152
Command AddContact qui est en fait un handler onClick directement "bindé"
149153
dans une vue XML avec binding:onClick="AddContact") :
150154

151-
public class ContactManagerModel {
152-
private Activity mContext;
153-
154-
public CursorSource<ContactRowModel> ContactList = new CursorSource<ContactRowModel>(ContactRowModel.class, new Factory());
155-
156-
public BooleanObservable ShowInvisible = new BooleanObservable(false);
157-
158-
public Command PopulateList = new Command(){
159-
public void Invoke(View view, Object... args) {
160-
populateContactList();
161-
}
162-
};
163-
public Command AddContact = new Command(){
164-
public void Invoke(View view, Object... args) {
165-
launchContactAdder();
166-
}
167-
};
168-
169-
private void populateContactList() {
170-
// Build adapter with contact entries
171-
Cursor cursor = getContacts();
172-
ContactList.setCursor(cursor);
173-
}
174-
}
155+
{% highlight java %}
156+
public class ContactManagerModel {
157+
private Activity mContext;
158+
159+
public CursorSource<ContactRowModel> ContactList = new CursorSource<ContactRowModel>(ContactRowModel.class, new Factory());
160+
161+
public BooleanObservable ShowInvisible = new BooleanObservable(false);
162+
163+
public Command PopulateList = new Command(){
164+
public void Invoke(View view, Object... args) {
165+
populateContactList();
166+
}
167+
};
168+
public Command AddContact = new Command(){
169+
public void Invoke(View view, Object... args) {
170+
launchContactAdder();
171+
}
172+
};
173+
174+
private void populateContactList() {
175+
// Build adapter with contact entries
176+
Cursor cursor = getContacts();
177+
ContactList.setCursor(cursor);
178+
}
179+
}
180+
{% endhighlight %}
175181

176182
Les vues correspondent aux layout en xml avec des namespaces binding: (ce qui
177183
rend l'édition des vues xml incompatibles avec l'éditeur intégrér au plugin eclipse) :
178184

179-
<LinearLayout xmlns:android="http://...." xmlns:binding="http://www.gueei.com/android-binding/" ..>
180-
<TextView binding:text="FirstName" ...
185+
{% highlight xml %}
186+
<LinearLayout xmlns:android="http://...." xmlns:binding="http://www.gueei.com/android-binding/" ..>
187+
<TextView binding:text="FirstName" ...
188+
{% endhighlight %}
181189

182190
Les Activity android se chargent de faire le lien entre le modèle et la vue
183191
et absolument rien d'autre. Cela permet donc de bien séparer la partie
@@ -186,14 +194,18 @@ présentation de la partie fonctionnelle.
186194
Le framework est très prometteur et permet d'effectuer de la validation de
187195
modèle à l'aide d'annotations sur les champs du modèle :
188196

189-
@Required(ErrorMessage="You must put the login name! (you can try Jean-Michel)")
190-
public final Observable<CharSequence> Login;
197+
{% highlight java %}
198+
@Required(ErrorMessage="You must put the login name! (you can try Jean-Michel)")
199+
public final Observable<CharSequence> Login;
200+
{% endhighlight %}
191201

192202
ou encore :
193203

194-
@Required
195-
@EqualsTo(Observable="Password")
196-
public final Observable<CharSequence> ConfirmPassword;
204+
{% highlight java %}
205+
@Required
206+
@EqualsTo(Observable="Password")
207+
public final Observable<CharSequence> ConfirmPassword;
208+
{% endhighlight %}
197209

198210
Un framework à définitivement tester, ainsi que son intégration avec RoboGuice.
199211

@@ -209,27 +221,31 @@ vie pour dialoguer avec des APIs REST. Android Annotations a d'ailleurs intégr
209221
RestTemplate dans ses annotations et ça devient vraiment sympa à coder.
210222
Il suffit de coder son service REST :
211223

212-
@Rest("http://monserveur.fr/api")
213-
public interface MonServiceRest {
214-
215-
@Get("/item/{id}")
216-
@Accept(MediaType.APPLICATION_JSON)
217-
Item getItem(long id);
218-
}
224+
{% highlight java %}
225+
@Rest("http://monserveur.fr/api")
226+
public interface MonServiceRest {
227+
228+
@Get("/item/{id}")
229+
@Accept(MediaType.APPLICATION_JSON)
230+
Item getItem(long id);
231+
}
232+
{% endhighlight %}
219233

220234
Puis dans sa vue d'injecter le service et de l'utiliser ensuite :
221235

222-
@RestService
223-
MonServiceRest monServiceRest;
224-
225-
@AfterViews
226-
@Background
227-
void init() {
228-
item = monServiceRest.getItem(2L);
229-
if(item != null) {
230-
showItem();
231-
}
232-
}
236+
{% highlight java %}
237+
@RestService
238+
MonServiceRest monServiceRest;
239+
240+
@AfterViews
241+
@Background
242+
void init() {
243+
item = monServiceRest.getItem(2L);
244+
if(item != null) {
245+
showItem();
246+
}
247+
}
248+
{% endhighlight %}
233249

234250
En 10 lignes, j'ai codé un bout d'appli qui récupère directement mes données
235251
depuis mon serveur. A faire sans l'aide de framework, c'est beaucoup plus long
@@ -250,8 +266,12 @@ Android. Cela peut devenir pratique si on commence à utiliser beaucoup de libra
250266

251267
Pour builder mon appli :
252268

253-
mvn install
269+
{% highlight sh %}
270+
mvn install
271+
{% endhighlight %}
254272

255273
Pour déployer l'application sur un terminal :
256274

257-
mvn android:deploy
275+
{% highlight sh %}
276+
mvn android:deploy
277+
{% endhighlight %}

0 commit comments

Comments
 (0)