Skip to content

Commit 8b54007

Browse files
committed
fixed markdown code blocks indentation
1 parent e18f2d0 commit 8b54007

File tree

1 file changed

+75
-73
lines changed

1 file changed

+75
-73
lines changed

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

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ 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-
> @DrawableRes(R.drawable.logo)
59-
> Drawable logo;
60-
> @SystemService
61-
> SearchManager searchManager;
62-
> }
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+
}
6365

6466
Le framework fonctionne par génération de code à la compilation (JAPT) en
6567
créant des classes suffixées d'un _. Une activity MyActivity devient donc
@@ -88,19 +90,19 @@ Projet très jeune et peu documenté, pas du tout prêt à être utilisé.
8890

8991
ORM basé sur des annotations :
9092

91-
> @DatabaseTable(tableName = "accounts")
92-
> public class Account {
93-
> @DatabaseField(id = true)
94-
> private String name;
95-
>
96-
> @DatabaseField(canBeNull = false)
97-
> private String password;
98-
> ...
99-
> Account() {
100-
> // all persisted classes must define a no-arg constructor with at least package visibility
101-
> }
102-
> ...
103-
> }
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+
}
104106

105107
Il s'intègre bien dans les applications Android (l'API pour Android est là).
106108
L'intégration avec RoboGuice est possible et on obtient alors une stack qui
@@ -146,36 +148,36 @@ Le model gère les données et les hanlders (on peut voir la déclaration de la
146148
Command AddContact qui est en fait un handler onClick directement "bindé"
147149
dans une vue XML avec binding:onClick="AddContact") :
148150

149-
> public class ContactManagerModel {
150-
> private Activity mContext;
151-
>
152-
> public CursorSource<ContactRowModel> ContactList = new CursorSource<ContactRowModel> (ContactRowModel.class, new Factory());
153-
>
154-
> public BooleanObservable ShowInvisible = new BooleanObservable(false);
155-
>
156-
> public Command PopulateList = new Command(){
157-
> public void Invoke(View view, Object... args) {
158-
> populateContactList();
159-
> }
160-
> };
161-
> public Command AddContact = new Command(){
162-
> public void Invoke(View view, Object... args) {
163-
> launchContactAdder();
164-
> }
165-
> };
166-
>
167-
> private void populateContactList() {
168-
> // Build adapter with contact entries
169-
> Cursor cursor = getContacts();
170-
> ContactList.setCursor(cursor);
171-
> }
172-
> }
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+
}
173175

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

177-
> <LinearLayout xmlns:android="http://...." xmlns:binding="http://www.gueei.com/android-binding/" ..>
178-
> <TextView binding:text="FirstName" ...
179+
<LinearLayout xmlns:android="http://...." xmlns:binding="http://www.gueei.com/android-binding/" ..>
180+
<TextView binding:text="FirstName" ...
179181

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

187-
> @Required(ErrorMessage="You must put the login name! (you can try Jean-Michel)")
188-
> public final Observable<CharSequence> Login;
189+
@Required(ErrorMessage="You must put the login name! (you can try Jean-Michel)")
190+
public final Observable<CharSequence Login;
189191

190192
ou encore :
191193

192-
> @Required
193-
> @EqualsTo(Observable="Password")
194-
> public final Observable<CharSequence> ConfirmPassword;
194+
@Required
195+
@EqualsTo(Observable="Password")
196+
public final Observable<CharSequence> ConfirmPassword;
195197

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

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

210-
> @Rest("http://monserveur.fr/api")
211-
> public interface MonServiceRest {
212-
>
213-
> @Get("/item/{id}")
214-
> @Accept(MediaType.APPLICATION_JSON)
215-
> Item getItem(long id);
216-
> }
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+
}
217219

218220
Puis dans sa vue d'injecter le service et de l'utiliser ensuite :
219221

220-
> @RestService
221-
> MonServiceRest monServiceRest;
222-
>
223-
> @AfterViews
224-
> @Background
225-
> void init() {
226-
> item = monServiceRest.getItem(2L);
227-
> if(item != null) {
228-
> showItem();
229-
> }
230-
> }
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+
}
231233

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

249251
Pour builder mon appli :
250252

251-
> mvn install
253+
mvn install
252254

253255
Pour déployer l'application sur un terminal :
254256

255-
> mvn android:deploy
257+
mvn android:deploy

0 commit comments

Comments
 (0)