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

Android Discussion :

La méthode notifyDataSetChanged ne fonctionne pas pour un BaseAdapter


Sujet :

Android

  1. #1
    Membre régulier Avatar de titou624
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 152
    Points : 87
    Points
    87
    Par défaut La méthode notifyDataSetChanged ne fonctionne pas pour un BaseAdapter
    Bonjour à tous !

    J'ai une classe ProduitsListAdapter qui extends BaseAdapter.

    Je voulais savoir si il fallait surcharger la méthode notifyDataSetChanged ou pas car lorsque je l'appel, rien ne se passe et ma listview reste identique.

    Pour qu'elle change, je suis obligé de faire ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    produits = sp.getListeProduits(devis.getId());        
    pla = new ProduitsListAdapter(getApplicationContext(), produits);
    lv_produits.setAdapter(pla);
    Ce qui m'oblige à effectuer une requête sur le web pour chaque produit...

    Merci beaucoup !

  2. #2
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Salut,

    Chez moi ce genre de méthode fonctionne
    Pourrais-tu nous donner un code un peu plus complet pour bien comprendre pourquoi dans ton cas ça ne fonctionne pas.

    Merci.

  3. #3
    Membre régulier Avatar de titou624
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 152
    Points : 87
    Points
    87
    Par défaut
    Sans problèmes !

    Voici mon adapter:

    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
     
    package com.trebbe.scanner.renderer;
     
    import java.io.IOException;
    import java.util.ArrayList;
     
    import org.json.JSONException;
     
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
     
    import com.trebbe.scanner.Produit;
    import com.trebbe.scanner.ProduitDevis;
    import com.trebbe.scanner.R;
     
    public class ProduitsListAdapter extends BaseAdapter {
     
        public  ArrayList<ProduitDevis> produitsDevis;
        public  ArrayList<Produit> produits;
        private LayoutInflater inflater;
     
        public ProduitsListAdapter(Context context, ArrayList<ProduitDevis> produits) throws JSONException, IOException {        
            this.produits = new ArrayList<Produit>();
            this.produitsDevis = new ArrayList<ProduitDevis>();
            inflater = LayoutInflater.from(context);
            for (ProduitDevis produit : produits) {            
                this.produitsDevis.add(produit);            
                this.produits.add(new Produit(produit.getCodeBarre()));
            }
     
        }
     
        @Override
        public int getCount() {        
            return this.produits.size();
        }
     
        @Override
        public void notifyDataSetChanged() {
            Log.e("barcode","Actualisaation de la liste notifyDataSetChanged");
            super.notifyDataSetChanged();
        }
     
        @Override
        public ProduitDevis getItem(int position) {
            return this.produitsDevis.get(position);
        }
     
        @Override
        public long getItemId(int position) {
            return getItem(position).getId();
        }
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {    
            ProduitView pv;
            if (convertView == null) {
                pv = new ProduitView();
                convertView = inflater.inflate(R.layout.produit_list_renderer, null);
     
                pv.iv_image = (ImageView)convertView.findViewById(R.id.iv_imageProd);
                pv.tv_prix = (TextView) convertView.findViewById(R.id.tv_prix);
                pv.tv_barcode = (TextView) convertView.findViewById(R.id.tv_barcode);
                pv.tv_label = (TextView)convertView.findViewById(R.id.tv_label);
                pv.tv_prix = (TextView)convertView.findViewById(R.id.tv_prix);
                pv.tv_qte = (TextView)convertView.findViewById(R.id.tv_qte);
                convertView.setTag(pv);
     
            } else {
                pv = (ProduitView) convertView.getTag();
            }
            pv.tv_prix.setText(Float.toString(produits.get(position).getPrix()));
            pv.tv_barcode.setText(produitsDevis.get(position).getCodeBarre());
            pv.tv_label.setText(produits.get(position).getLibelle());
            pv.tv_qte.setText(produitsDevis.get(position).getQte()+"");        
            pv.iv_image.setImageBitmap(produits.get(position).getImage());        
            return convertView;
        }
     
    }
    Et voici la classe de mon activity:

    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
     
    package com.trebbe.scanner;
     
    import java.io.IOException;
    import java.text.ParseException;
    import java.util.ArrayList;
     
    import org.json.JSONException;
     
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
     
    import com.trebbe.scanner.provider.SqliteProvider;
    import com.trebbe.scanner.renderer.ProduitsListAdapter;
     
    public class ActivityAjoutProduits extends Activity {
     
        private static final int CODE_SCANNER = 10;
        private static final int CODE_DESCRIPTION_PRODUIT = 11;
     
        private TextView tv_client;
        private ListView lv_produits;
     
        private ProduitsListAdapter pla;
        private Devis devis;
        private ArrayList<ProduitDevis> produits;
        private SqliteProvider sp;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ajout_produit);
     
            sp = new SqliteProvider(getApplicationContext());
            //On récupère l'objet Bundle envoyé par l'autre Activity
            Bundle objetbunble  = this.getIntent().getExtras();         
     
            //On récupère les données du Bundle
            long idDevis = 0;
            if (objetbunble != null && objetbunble.containsKey("idDevis")) { 
                idDevis = objetbunble.getLong("idDevis");
            }
            else
                finish();
            Devis d = new Devis();
            try {
                devis = sp.getDevis(idDevis);
            } catch (NumberFormatException e) {            
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
     
            tv_client = (TextView)findViewById(R.id.tv_client);
            tv_client.setText(devis.getClient().getNom()+" "+devis.getClient().getPrenom());
     
            lv_produits = (ListView)findViewById(R.id.lv_produits);    
     
            findViewById(R.id.bt_scan).setOnClickListener(bt_scanClick);
            if(lv_produits.getAdapter()!=null && lv_produits.getAdapter().getCount()==0)
                return;
            produits = sp.getListeProduits(devis.getId());        
            try {
                pla = new ProduitsListAdapter(getApplicationContext(), produits);
                lv_produits.setAdapter(pla);
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
     
        private Button.OnClickListener bt_scanClick = new Button.OnClickListener(){
     
            @Override
            public void onClick(View v) {
                lancerScan();
            }
     
        };
     
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == CODE_SCANNER) {
     
                if (resultCode == RESULT_OK) {
                    String idProduit = intent.getStringExtra("SCAN_RESULT");
                    if(idProduit!="")
                        lancerDescriptionProduit(idProduit);
                } 
                else 
                    if (resultCode == RESULT_CANCELED) {
     
                    }
            }
     
            if (requestCode == CODE_DESCRIPTION_PRODUIT) {
                if(resultCode == RESULT_OK){                
     
                    // Si le produit correspond, on l'ajoute a la base                    
     
                    produits.add(new ProduitDevis(sp.addProduct(devis.getId(), intent.getLongExtra("idProduit", 0), intent.getIntExtra("qte_produit", 0))));
                    // Ne fonctionne pas pour l'instant
                    pla.notifyDataSetChanged();
                }
            }
        }
     
        private void updateDataProduits() throws JSONException, IOException{
     
            /*
             // Fonctionne avec ce codes
            produits = sp.getListeProduits(devis.getId());        
            try {
                pla = new ProduitsListAdapter(getApplicationContext(), produits);
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            lv_produits.setAdapter(pla);        
             */
        }
     
        private void lancerScan(){
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            startActivityForResult(intent, CODE_SCANNER);
        }
     
        private void lancerDescriptionProduit(String idProduit){
            Bundle objetbunble = new Bundle();
            objetbunble.putString("idProd", idProduit);
            Intent intentDesc = new Intent(getApplicationContext(), DescriptionProduit.class);
     
            //On affecte à l'Intent le Bundle que l'on a créé
            intentDesc.putExtras(objetbunble);
     
            //On démarre l'autre Activity
            startActivityForResult(intentDesc, CODE_DESCRIPTION_PRODUIT);
        }
    }
    Merci pour ta réponse !

  4. #4
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Points : 4 240
    Points
    4 240
    Par défaut
    Ok, je comprend mieux.
    En faite le NotityDataSedChanged permet d'indiquer au composant qui contient une liste (un adapter) que la liste à été mis à jour et que donc il donc prendre cette modification en compte (se rafraichir).

    ton algo actuellement c'est ça :
    contructeur :
    récupération des info dans le SQL
    creation d'un adapter avec la liste d'infos récupérées
    initialisation d'une listview avec l'adapter

    dans ton activityresult :
    ajout d'une donnée dans le SQL
    notification qu'il y a eu une modif dans l'adapter.

    Il faudrait que tu fasses ça :
    dans ton activityresult :
    ajout d'une donnée dans le SQL
    mise à jour de l'adapter avec la nouvelle liste d'info
    notification qu'il y a eu une modif dans l'adapter.
    Si c'est pas clair, n'hésites pas à demander plus d'informations.

  5. #5
    Membre régulier Avatar de titou624
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 152
    Points : 87
    Points
    87
    Par défaut
    Un grand merci à toi !

    Voici la fonction que j'ai rajouté dans ma classe adapter:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    public void addProduct(ProduitDevis prod) throws JSONException, IOException{
            this.produits.add(new Produit(prod.getCodeBarre()));
            this.produitsDevis.add(prod);
            this.notifyDataSetChanged();
        }
    Ca marche nickel !!!

    Encore merci !

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

Discussions similaires

  1. méthode OfType() ne fonctionne pas
    Par maa dans le forum C#
    Réponses: 7
    Dernier message: 21/01/2008, 09h12
  2. Fonction mysql qui ne fonctionne pas pour un ancien postgreIste
    Par floreasy dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 15/01/2008, 18h49
  3. La propriété Appearance ne fonctionne pas pour un commandbutton
    Par ludoche dans le forum VB 6 et antérieur
    Réponses: 9
    Dernier message: 23/11/2007, 13h54
  4. A:hover ne fonctionne pas pour mon menu css
    Par kaylah dans le forum Mise en page CSS
    Réponses: 5
    Dernier message: 13/03/2007, 15h02
  5. Réponses: 13
    Dernier message: 20/07/2004, 08h54

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