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 :

ListView avec RadioGroup


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    483
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 483
    Par défaut ListView avec RadioGroup
    Bonjour,

    Voici la situation : je souhaite afficher une liste d'informations où chaque information contient un radiogroup avec 3 radiobuttons.

    Cela donnerait :

    -----------------------
    lib1 - cat1
    btn1 btn2 btn3
    -----------------------
    lib2- cat2
    btn1 btn2 btn3
    -----------------------
    lib3 - cat2
    btn1 btn2 btn3
    -----------------------
    J'utilise donc un layout représentant une ligne de la liste :

    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#F4EDF4"
        android:orientation="horizontal" >
     
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#F4EDF4"
            android:orientation="horizontal" >
     
            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
     
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
     
                    <TextView
                        android:id="@+id/lib"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="lib" />
     
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=" - "
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="#000000" />
     
                    <TextView
                        android:id="@+id/cat"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="cat"
                        android:textColor="#000000"
                        android:textSize="26px" />
     
                    <TextView
                        android:id="@+id/id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="id"
                        android:visibility="gone" />
                </LinearLayout>
     
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
     
                    <RadioGroup
                        android:id="@+id/radioGroup"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >
     
                        <RadioButton
                            android:id="@+id/radioButton1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:checked="true"
                            android:text="Ok"
                            android:textColor="#000000" />
     
                        <RadioButton
                            android:id="@+id/radioButton2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:text="A reconduire"
                            android:textColor="#000000" />
     
                        <RadioButton
                            android:id="@+id/radioButton3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:text="Inutile"
                            android:textColor="#000000" />
                    </RadioGroup>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
     
        </LinearLayout>
    Et dans mon fichier java associé je souhaite savoir quel radiobutton a été modifié (lorsqu'un des radiobuttons est modifié bien sur).

    J'aimerais beaucoup avoir un peu d'aide de votre part, car là je suis bloqué depuis un moment là dessus (j'essaye différents tutos trouvés sur le net, en vain).

    merci

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    En utilisant RadioGroup.OnCheckedChangeListener sans doute

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ((RadioGroup)findViewById(R.id.radioGroup)).setOnCheckedChangeListener(...);

  3. #3
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    483
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 483
    Par défaut
    Voilà comment je remplit ma 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
     
    //Création de la ArrayList qui nous permettra de remplire la listView
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
     
    //On déclare la HashMap qui contiendra les informations pour un item
    HashMap<String, String> map;
     
    for(int i = 0; i < maliste.size(); i++)
    {
          map.put("lib", maliste.get(i).getLib);
          map.put("cat", maliste.get(i).getCat);
          listItem.add(map);
    }
    //Récupération de la listview créée dans le fichier main.xml
    monListView = (ListView) findViewById(R.id.monListView);
     
    //Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue
    SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,new String[] {"lib", "cat", "id"}, new int[] {R.id.lib, R.id.cat, R.id.id});
     
    //On attribut à notre listView l'adapter que l'on vient de créer
    monListView .setAdapter(mSchedule);
    J'arrive bien à afficher la liste contenant où chaque item contient 3 radioBoutons. Sauf que dès que j'utilise :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    monRadioGroup.setOnCheckedChangeListener(
                    new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                           //traitement
                        }
                    });
    Ca plante.

  4. #4
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Non mais il faut surcharger l'Adapter dans ce cas....
    Comme sont nom l'indique, "SimpleAdapter" est un adapter "simple", incapable de gérer des trucs complexes (comme les radio group)....

    Donc faire un Adapter à soit (qui éventuellement surcharger ArrayAdapter, ou autre), et surcharger getView() pour mettre le listener (et éventuellement un "tag" au radiogroup pour savoir la ligne qui a été modifiée).

  5. #5
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    483
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 483
    Par défaut
    Pourrait tu m'aider en me montrant des exemples de ce que tu m'expliques?

    Sinon, je vais chercher de mon côté voir comment mettre en place tout ça.

  6. #6
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Un truc genre:
    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
     
    public class MyActivity extends Activity implements RadioGroup.OnCheckedChangeListener 
    {
     
        public class MyAdapter extends ArrayAdapter
        {
              public MyAdapter(....) { super(...); }
     
              public View getView(int position)
              {
                  View ret = super.getView(position);
                  RadioGroup rgroup  = ((ViewGroup)ret).findViewById(R.id.radioGroup);
                  rgroup.setOnCheckedChangeListener(MyActivity.this);
                  rgroup.setTag(Integer.valueOf(position));
                  return ret;
              }
        }
     
     
        public void onCreate()
        {
               ....
               this.adapter = new MyAdapter();
               setListAdapter(this.adapter);
               ...
        }
     
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
              int position = Integer.parseInt(group.getTag());
              ...
        }
    }

Discussions similaires

  1. [VB.NET] Listview avec checkboxes
    Par Tiib_CD dans le forum Windows Forms
    Réponses: 8
    Dernier message: 05/12/2006, 09h23
  2. [Qt] ListView avec menu contextuel ?
    Par agent007se dans le forum Qt
    Réponses: 12
    Dernier message: 29/11/2006, 13h04
  3. Remplir une ListView avec le contenu d'une requête
    Par callo dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 19/10/2006, 08h44
  4. Bug ListView avec XP manifest
    Par Betcour dans le forum Delphi
    Réponses: 1
    Dernier message: 02/10/2006, 00h49
  5. Remplir une ListView avec une BD
    Par manikou dans le forum MFC
    Réponses: 1
    Dernier message: 17/05/2005, 09h48

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