Développez une application pour android étape 15 (Rajouter une listview dans mon formulaire pour aficher les ingredients)
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
A lire aussi:
- Développez une application pour android etape 7 (Affichage de la liste des recettes)
- Développez une application pour android etape 8 (Affichage d’une recette)
- Développez une application pour android etape 12 (rajouter des icônes dans le menu)
- Développez une application pour android etape 6 (Enregistrement dans une table)
- Développez une application pour android étape 13 (Ajouter des ingredients avec une boite de dialogue personnalisée)
Une nouvelle version de notre Widget est disponible sur Android Market, Cliquez ICI ou scannez le code barre pour ne plus rater aucunes news.
© Guy pour Android-France, 2009. |
Permalien |
Soyez le premier à réagir |
Add to
del.icio.us
Pas de commentaire
Pas encore de commentaire.
Désolé, les commentaires sont fermés pour le moment.