Dans mon étape 15 je vais rajouter à la place de ma zone de texte pour les ingrédients une listview qui affichera la liste des ingrédients, et un click sur l’item supprimera l’ingrédient sélectionné de ma liste.
D’abord je modifie mon xml main.xml pour rajouter une listview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layoutAnimation="@anim/layout_bottom_to_top_slide">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nom de la recette" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
/>
<Button android:id="@+id/ajcondiment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ajouter un condiment" />
<ListView
android:id="@android:id/list"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="180px"
android:layoutAnimation="@anim/layout_bottom_to_top_slide" />
<TextView android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="La recette" />
<EditText android:id="@+id/entry3"
android:layout_width="fill_parent"
android:layout_height="200px"
android:background="@android:drawable/editbox_background"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider" />
</LinearLayout>
</ScrollView> |
je rajoute donc
1 2 3 4 5 6 | <ListView
android:id="@android:id/list"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="180px"
android:layoutAnimation="@anim/layout_bottom_to_top_slide" /> |
a la place de
1 2 3 4 | <EditText android:id="@+id/entry2"
android:layout_width="fill_parent"
android:layout_height="90px"
android:background="@android:drawable/editbox_background" /> |
Dans ma classe principale je crée deux attributs de type tableau
1 2 3 4 | //tableau de string pour sauvegarder temporairement les ingredients private final ArrayList<String[]> aTableauIngredient= new ArrayList<String[]>(); //tableau de string temporaire pour l'aafichage dans notre liste view private final ArrayList<String> aTableauIngredientaff= new ArrayList<String>(); |
Et pour finir je modifie la classe OnReadyListener de ma classe principale pour afficher les info saisies dans ma boite de dialogue dans ma listview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | private class OnReadyListener implements Myclassdialog.ReadyListener,
AdapterView.OnItemClickListener, View.OnClickListener{
@Override
public void ready(String sUnite,String sQuantite,String sIngredient) {
String[] sNewtext=new String[]{sUnite,sQuantite,sIngredient};
String sNewtextaff="";
//tableau temporaire des informations
aTableauIngredient.add(sNewtext);
//tableau temporaire de l'affichage
aTableauIngredientaff.add(sQuantite+" "+sUnite+" "+sIngredient);
//affiche des ingredients de mon tableau temporaire dans ma listview
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
mRecetteList.setOnItemClickListener(this);
//TextView textbastmp2 = (TextView) findViewById(R.id.entry2);
//textbastmp2.setText(sNewtextaff);
}
// action sur le clik sur un item de ma listview
public void onItemClick(AdapterView parent, View v, int position, long id) {
//***********suppression des informations dans mon tableau********
aTableauIngredientaff.remove(position);
aTableauIngredient.remove(position);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
mRecetteList.setOnItemClickListener(this);
}
public void onClick(View v) {
int u=0;
}
} |
Cette étape me permet donc d’ajouter des ingredients dans un tableau plus facile à traiter pour la suite, également d’afficher une listview dans un formulaire ce qui me permet sur le click d’un item de supprimer un ingredient ajouté par erreur ou avec une erreur de saisie.

code source ici
Il est temps de mettre à profit les tutoriels d’Android France:
Passez à la vitesse supérieure et investissez quelques dizaines d’euros pour acquérir les connaissances qui vous feront gagner de l’argent avec vos applications rendez-vous sur notre boutique Android-france pour ces formations en vidéo


















26 mars 2010 à 17 h 14 min
Bonjour,
Quelqu’un connait il un moyen de rendre la hauteur du ListView ajustable dynamiquement en fonction du nombre d’items qu’il contient ?
La solution que j’ai trouvée est de faire :
mRecetteList.getLayoutParams().height = adapter2.getCount() * 75;
Afin d’ajuster la hauteur à chaque fois qu on ajoute ou supprime un item. Mais il y a t il une solution plus propre, ou bien une propriété des Layout ou ListView qui gère ce comportement ?
Merci pour votre aide.
Al