IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

Bouton delete dans listView


Sujet :

Composants graphiques Android

  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut Bouton delete dans listView
    Bonjour à tous,

    Je suis sur une application en ce moment et je bloque sur l'evenement clic d'un bouton dans les lignes d'une listView. Etant débutant j'ai cherché sur plusieurs forums et tutos mais je ne comprends vraiment rien.

    Voici mon code XML de la page
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    <?xml version="1.0" encoding="utf-8"?>
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff8824"
        android:gravity="center"
        android:weightSum="1">
     
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="402dp"
            android:layout_height="337dp"
            android:layout_weight="0.69">
     
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/listViewPrets" />
        </LinearLayout>
    </LinearLayout>
    Voici mon code XML de la ligne du listView
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
     
    <?xml version="1.0" encoding="utf-8"?>
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">
     
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1">
     
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="0.90">
     
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/nomObjet"
                        android:id="@+id/nomObjet"
                        android:textSize="12sp" />
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:id="@+id/leNomObjet"
                        android:textSize="12sp"
                        android:layout_marginLeft="20dp"
                        android:clickable="false" />
                </LinearLayout>
     
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/nomPersonne"
                        android:id="@+id/nomPersonne"
                        android:textSize="12sp" />
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:id="@+id/leNomPersonne"
                        android:layout_marginLeft="20dp"
                        android:textSize="12sp"
                        android:clickable="false" />
                </LinearLayout>
     
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/numPersonne"
                        android:id="@+id/numPersonne"
                        android:textSize="12sp" />
     
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:id="@+id/leNumPersonne"
                        android:textSize="12sp"
                        android:layout_marginLeft="20dp"
                        android:clickable="false" />
                </LinearLayout>
            </LinearLayout>
     
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="82dp">
     
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageButtonSupprimer"
                    android:layout_gravity="center_vertical"
                    android:src="@android:drawable/ic_delete"
                    android:contentDescription="@string/imageSuppr" />
            </LinearLayout>
        </LinearLayout>
     
    </LinearLayout>
    et enfin le code java de la page en question
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
     
    package com.example.app;
     
    import android.content.Intent;
    import android.database.Cursor;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
     
    import java.util.ArrayList;
    import java.util.HashMap;
     
    public class ConsulterPret extends ActionBarActivity {
     
        PretBDD unPretBDD;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.consulterpret);
     
            ListView listViewPret = (ListView) findViewById(R.id.listViewPrets);
            unPretBDD = new PretBDD(this);
            unPretBDD.open();
            Cursor unCursor = unPretBDD.getLesInfos();
     
            ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
     
            HashMap<String, String> map;
     
            if(unCursor.moveToFirst())
            {
                int nb = unCursor.getCount();
                for(int i=0;i<nb;i++)
                {
                    String obj = unCursor.getString(0);
                    String pers = unCursor.getString(1);
                    String numero = unCursor.getString(2);
     
     
                    map = new HashMap<String, String>();
                    map.put("leNomObjet", obj);
                    map.put("leNomPersonne", pers);
                    map.put("leNumPersonne", numero);
                    listItem.add(map);
                    unCursor.moveToNext();
                }
            }
     
            SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.lignelistviewpret,
                    new String[] {"leNomObjet", "leNomPersonne", "leNumPersonne"}, new int[] {R.id.leNomObjet, R.id.leNomPersonne, R.id.leNumPersonne});
     
            listViewPret.setAdapter(mSchedule);
     
            unPretBDD.close();
     
        }
     
    }
    PretBDD est une classe qui permet l'accès à une bdd SQLite.

    Voilà, ma question est : si je veux que lors d'un clic sur le bouton supprimer que contiennent les lignes la ligne du listView soir supprimer (en même temps que celle que j'ai dans la bdd) qu'est-ce que je dois faire ?

    Merci à tous

  2. #2
    Rédacteur
    Avatar de David55
    Homme Profil pro
    Ingénieur informatique
    Inscrit en
    Août 2010
    Messages
    1 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2010
    Messages : 1 542
    Par défaut
    Bonjour,

    Pour supprimer ta ligne, il faut supprimer ton élément dans ton tableau listItem et faire un notifyDataSetChanged sur ton adapter.

    Attention au linearlayout inutile dans tes xml.

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut
    Merci pour cette réponse rapide.

    Merci pour cette aide, le problème surtout c'est que je ne savais pas trop par quoi commencer.

    J'ai lu qu'il fallait hériter la classe de l'adapter pour créer un setOnClickListener sur le bouton en question (ce que j'ai réussi à faire et me donne le code suivant).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package com.example.app;
     
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
     
    import java.util.List;
    import java.util.Map;
     
    public class PretAdapter extends SimpleAdapter
    {
        Context unContext;
     
        public PretAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            this.unContext=context;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View MyView = convertView;
     
            if ( convertView == null )
            {
                LayoutInflater inflater = LayoutInflater.from(this.unContext);
                MyView = inflater.inflate(R.layout.lignelistviewpret, null);
     
                ImageButton btnSuppr = (ImageButton)MyView.findViewById(R.id.imageButtonSupprimer);
                TextView obj = (TextView)MyView.findViewById(R.id.leNomObjet);
                TextView pers = (TextView)MyView.findViewById(R.id.leNomPersonne);
                TextView num = (TextView)MyView.findViewById(R.id.leNumPersonne);
     
                btnSuppr.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
     
     
                    }
     
                });
            }
            return MyView;
        }
    }
    Le problème maintenant c'est que les TextView qui étaient initialisées ici

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.lignelistviewpret,
                    new String[] {"leNomObjet", "leNomPersonne", "leNumPersonne"}, new int[] {R.id.leNomObjet, R.id.leNomPersonne, R.id.leNumPersonne});
    ne le sont plus maintenant.

    Que dois-je faire pour remettre les bons string à ces TextView ?

  4. #4
    Rédacteur
    Avatar de David55
    Homme Profil pro
    Ingénieur informatique
    Inscrit en
    Août 2010
    Messages
    1 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2010
    Messages : 1 542
    Par défaut
    C'est pas compliqué. Il suffit d'utiliser ton tableau dans ton adapteur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class PretAdapter extends SimpleAdapter
    {
        Context unContext;
    	private List<? extends Map<String, ?>> data;
     
        public PretAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            this.unContext=context;
    		// On récupère le tableau
    		this.data = data;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View MyView = convertView;
     
            if ( convertView == null )
            {
                LayoutInflater inflater = LayoutInflater.from(this.unContext);
                MyView = inflater.inflate(R.layout.lignelistviewpret, null);
     
                ImageButton btnSuppr = (ImageButton)MyView.findViewById(R.id.imageButtonSupprimer);
                TextView obj = (TextView)MyView.findViewById(R.id.leNomObjet);
                TextView pers = (TextView)MyView.findViewById(R.id.leNomPersonne);
                TextView num = (TextView)MyView.findViewById(R.id.leNumPersonne);
     
    			// TODO : On initialise les textviews en récupérant les valeurs dans data
    			// obj.setText(...);
    			// pers.setText(...);
    			// num.setText(...);
     
                btnSuppr.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
     
     
                    }
     
                });
            }
            return MyView;
        }
    }
    EDIT : pour récupérer tes valeurs de ta liste tu devras faire quelques chose comme ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    obj.setText(data.get(position).get ("leNomObjet"));
    pers.setText(data.get(position).get ("leNomPersonne"));
    num.setText(data.get(position).get ("leNumPersonne"));
    EDIT 2 : pour supprimer un élément lors du clic, place ce bout de code dans ton onClick:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    // On supprime de la liste
    data.remove (position);
    // On rafraichit la liste
    notifyDataSetChanged ();
    Attention tu dois modifier la variable "position" en final.

  5. #5
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut
    D'accord merci.

    J'ai un autre soucis sur la lecture du tableau par contre.

    Je n'arrive pas à le lire si je fais ça

    c'est normal ?

  6. #6
    Rédacteur
    Avatar de David55
    Homme Profil pro
    Ingénieur informatique
    Inscrit en
    Août 2010
    Messages
    1 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2010
    Messages : 1 542
    Par défaut
    cf mon post juste au dessus que j'ai édité.

  7. #7
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut
    au temps pour moi...

  8. #8
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut
    quand je fais

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    data.remove (position);
    notifyDataSetChanged ();
    c'est toujours la dernière ligne qui est supprimée quelque soit le bouton sur lequel je clique

  9. #9
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 22
    Par défaut
    position est toujours égale à 0

  10. #10
    Membre éprouvé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2012
    Messages
    129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 129
    Par défaut
    Pour supprimer un élément dans ta liste met un listener ' setOnClickItem ' un truc du genre sur ta listView et place le code que la personne ta donné. Et grâce à ceux-ci tu pourra supprimer l'élément de ta liste.

    Ce sera plus propre que de mettre un listener dans ton adapter

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. inserer bouton dans listview en vb net
    Par sihammaster dans le forum VB.NET
    Réponses: 0
    Dernier message: 01/02/2010, 10h45
  2. Bouton et TextBox dans ListView
    Par matdur dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 23/03/2009, 10h31
  3. [C#] Lier des boutons radio dans une application mobile
    Par Loïc56 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 22/04/2005, 13h00
  4. aprés un delete dans un formulaire
    Par Valarauko dans le forum XMLRAD
    Réponses: 2
    Dernier message: 11/01/2005, 14h02
  5. Delete dans la propriété Rows d'un TStringGrid ???
    Par Neilos dans le forum C++Builder
    Réponses: 2
    Dernier message: 26/02/2004, 23h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo