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 :

Ajouter un TableRow dynamiquement


Sujet :

Android

  1. #1
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut Ajouter un TableRow dynamiquement
    Bonjour,

    Je tente d'ajouter dynamiquement une ligne dans un TableLayout, mais sans succès (rien n'apparait à l'affichage, pas d'erreur).

    Voila le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
         <TableRow
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
     
              <Button android:text="Static Button"/>
         </TableRow>
    </TableLayout>
    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
    TableLayout tableMain= (TableLayout) findViewById(R.id.tableMain);
            TableRow tr = new TableRow(this);
            tr.setId(5);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));   
     
            ImageView icon = new ImageView(this);
            icon.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            icon.setImageResource(R.drawable.image);
            tr.addView(icon);
     
            TextView texteCol = new TextView(this);
            texteCol.setText("mon texte");
            texteCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(texteCol);
     
            TextView limitePrixCol = new TextView(this);
            limiteCol.setText(String.valueOf(15.5));
            limiteCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(limiteCol);
     
            tableMain.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

    Cela fonctionne-t-il chez vous?
    Merci

  2. #2
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    A priori, d'après le hierachy viewer mobn tableRow est bien là, mais invisible...
    J'ai un layout_startMargin et layout_endMargin de -2147483648 c'est peut être ça?

  3. #3
    Expert éminent

    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
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Question....

    Pourquoi ne pas utiliser une ressource XML pour chaque row (TableRow) et dans le code un "Inflater" pour créer l'arborescence des views... comme ca tu as le meilleur des deux mondes: les ressources et la dynamique ...
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  4. #4
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2011
    Messages
    144
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2011
    Messages : 144
    Points : 118
    Points
    118
    Par défaut
    Si ça peut t'aider, j'ai fais quelque chose de similaire, et c'est bien pratique comme le dit nicroman.


    Code java : 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
    public void onCreate(Bundle savedInstanceState)
    {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.recap);
     
    		...
     
    		LayoutInflater inflater = getLayoutInflater();
    		TableLayout table = (TableLayout) findViewById(R.id.tableLayoutRecap);
     
     
    		// construction dynamique de la vue, selon les absents
    		for (int i = 0; i < nbAbsents; i++)
    		{
     
    			TableRow tr = (TableRow) inflater.inflate(R.layout.recap_tablerow, null); 
     
    			((TextView) tr.findViewById(R.id.textRecapAbsent)).setText(tabAbsents_name.get(i));
     
    			table.addView(tr);
     
    		}
    }

    Code xml de la vue (recap.xml) : 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
    <?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="fill_parent"
        android:orientation="vertical" >
     
        ...
     
        <TableLayout
            android:id="@+id/tableLayoutRecap"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TableLayout>
     
        ...
     
    </LinearLayout>

    Code pour le TableRow (recap_tablerow.xml) : 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
    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
     
        <Button
            android:id="@+id/btRecapAbsent"
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:text="X"
            android:textColor="#F00"
            android:textStyle="bold" />
     
        <TextView 
            android:id="@+id/textRecapAbsent"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />
     
    </TableRow>

  5. #5
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    Merci beaucoup à vous deux, je devrais pourvoir me débrouiller avec ça!

  6. #6
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    C'est parfait tout fonctionne, mais j'aurais une question:

    J'ajoute donc par inflation X fois un EditText (par ex) dont l'id est "MonEditText".

    Comment faire pour supprimer TOUS ces EditText par la suite?

    Merci

    EDIT: avec getChild, GetChildAt et remove c'est tout bon.

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

Discussions similaires

  1. [VB.Net] Ajout de composant dynamique
    Par le Daoud dans le forum ASP.NET
    Réponses: 4
    Dernier message: 13/02/2006, 11h21
  2. [TPageControl] Ajouter un tabsheet dynamiquement
    Par qi130 dans le forum Langage
    Réponses: 6
    Dernier message: 29/09/2005, 20h57
  3. [VB.NET] Ajout de controles dynamiquement
    Par A77 dans le forum ASP.NET
    Réponses: 2
    Dernier message: 25/04/2005, 09h00
  4. [C#] ajouter une image dynamiquement
    Par h_imane dans le forum ASP.NET
    Réponses: 4
    Dernier message: 21/04/2004, 11h27
  5. ajouter un champ dynamiquement à une instance de table
    Par maniack dans le forum Bases de données
    Réponses: 2
    Dernier message: 28/02/2004, 23h58

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