Jump to content

Affichage dans un menu déroulant ![Résolu]


flabb

Recommended Posts

Bonjour,

Voici ma question !

J'ai plusieurs ArrayList qui contiennent une liste d'objet (dans mon cas un objet Carte !)

et je regroupe cela dans une "SUPER" ArrayList qui englobe le tout

// contient les differents Dico
ArrayList> listeDico = new ArrayList>();
// chaque Dico
ArrayList Dico1 = new ArrayList();
ArrayList Dico2 = new ArrayList();
ArrayList Dico3 = new ArrayList();

listeDico.add(Dico1);
listeDico.add(Dico2);
listeDico.add(Dico3);

Et maintenant je crée mon ArrayAdapter

ArrayAdapter> adapter = new ArrayAdapter>(this,android.R.layout.simple_spinner_item,listeDico);

le problème c'est que par defaut , aucun String n'est associé a une ArrayList !

mon menu déroulant fontionne je peux bien choisir, mais n'afficher que des "[]" "[]" pour chaque élément de la liste alors que j'aimerai Dico1, Dico2, Dico3 ( pas facile de choisir :lol: )

J'ai contourné le problème en créant une 2ème ArrayListe qui ne contient que les noms , mais je trouve cela fort peut élégant !

que faire ?

il y a pas moyen de définir un string a une ArraList ? ( comme une Hashtable ?)

réécrire un méthode qui remplace le constructeur de ArrayAdapter personnalisé ? si oui comment ?

Merci !!

Edited by flabb
Link to comment
Share on other sites

J'ai trouver la solution à mon problème !!

Le code source d'Android dit :

* However the TextView is referenced, it will be filled with the toString() of each object in

* the array. You can add lists or arrays of custom objects. Override the toString() method

* of your objects to determine what text will be displayed for the item in the list.

Donc comme mesArrayList Dico1 Dico2 et Dico3 ne sont pas une instance de CharSequence, il appelle Monobjet.toString(), qui n'existe pas dans ArrayAdapter et qui donne donc [] !

donc si je créée une méthode .toString() pour un objet ArrayAdapter c'est ok !!

Je met le code ici si ça intéresse du monde !!

package ch.fabbet.android.VocabuloCarte;

import java.util.ArrayList;

class ArrayListText extends ArrayList{

// Pour être certain de la concordance des versions !
private static final long serialVersionUID = 1L;
private String text;

   public ArrayListText(String string){
       this.text = string;
   }

   @Override
    public String toString(){
       String string= this.text;
        return string;
    }
}

Edited by flabb
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...