Jump to content

[Résolu] Ajout TableRow en boucle for


Mc Flurry

Recommended Posts

Bonjour,

J'essaie d'ajouter des ligne à mon tableLayout en dynamique depuis le code Java.

Jusqu'à présent ajouter une seule ligne ne pose aucun problème, mais je me casse les dents lorsqu'il sagit d'en ajouter plusieurs... l'application m'éjecte dès le lancement.

Voici mon code XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"  
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" > 

  <ImageView
   	android:id="@+id/logo"
  		android:layout_height="wrap_content"
  		android:layout_width="fill_parent"
	android:src="@drawable/logo" 
	android:padding="5px" />

<TableLayout
    android:id="@+id/details_tl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:shrinkColumns="0,1"	    
    android:padding="2px" >

     <TableRow>
        <TextView
            android:layout_column="0"
            android:layout_height="fill_parent"
            android:text="Nom"
            android:textColor="#ff444444"
            android:background="#1C8DAF"
            android:gravity="center"
            android:padding="2dip"
            android:layout_margin="2dip"/>	            

        <TextView
            android:layout_column="1" 
            android:layout_height="fill_parent" 
            android:text="Prenom"
            android:background="#1C8DAF"
            android:textColor="#ff444444"
            android:gravity="center"
            android:padding="2dip" 
            android:layout_margin="2dip"/>	        
   	</TableRow>    	
</TableLayout>

</LinearLayout>

Voici mon code Java :

package com.application.projet;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class DetailsActivity extends Activity {

private static final String TAG = "DetailsActivity";

public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.details_layout);

	TableLayout tl = (TableLayout) findViewById(R.id.details_tl);
	TableRow tr = new TableRow(this);
	tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
			LayoutParams.WRAP_CONTENT));

	LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
			LayoutParams.FILL_PARENT);
	layoutParams.setMargins(2, 2, 2, 2);

	TextView nom = new TextView(this);
	nom.setBackgroundColor(Color.LTGRAY);
	nom.setTextColor(Color.DKGRAY);
	nom.setGravity(Gravity.CENTER);
	nom.setPadding(2, 2, 2, 2);
	nom.setLayoutParams(layoutParams);

	TextView prenom = new TextView(this);
	prenom.setBackgroundColor(Color.LTGRAY);
	prenom.setTextColor(Color.DKGRAY);
	prenom.setGravity(Gravity.CENTER);
	prenom.setPadding(2, 2, 2, 2);
	prenom.setLayoutParams(layoutParams);

	for (int i = 0; i < 2; i++) {
		nom.setText("toto");
		tr.addView(nom);

		prenom.setText("AAA");
		tr.addView(prenom);

		tl.addView(tr, layoutParams);
	}
}
}

Lorsque que j'éxecute ce code sans boucle tout fonctionne mais lorsque que je rejoute cette boucle, l'application m'éjecte et le log cat affiche

05-02 09:53:29.193: ERROR/AndroidRuntime(816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.projet/com.application.projet.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.projet/com.application.projet.DetailsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-02 09:53:29.193: ERROR/AndroidRuntime(816): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.projet/com.application.projet.DetailsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-02 09:53:29.193: ERROR/AndroidRuntime(816):     at com.application.projet.DetailsActivity.onCreate(DetailsActivity.java:48)

Comment ajoute-t-on des lignes avec une boucle for ?

Merci de votre réponse.

Edited by Mc Flurry
Link to comment
Share on other sites

Tu as une erreur parce que tu insères les mêmes TextView/TableRow plusieurs fois dans des éléments différents. Il te faut instancier un TableRow et des TextView pour chaque ligne que tu insères

Quelque chose comme ça:


       public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.details_layout);

               TableLayout tl = (TableLayout) findViewById(R.id.details_tl);
               for (int i = 0; i < 2; i++) {
                       TableRow tr = new TableRow(this);
                       tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                               LayoutParams.WRAP_CONTENT));

                       LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
                               LayoutParams.FILL_PARENT);
                       layoutParams.setMargins(2, 2, 2, 2);

                       TextView nom = new TextView(this);
                       nom.setBackgroundColor(Color.LTGRAY);
                       nom.setTextColor(Color.DKGRAY);
                       nom.setGravity(Gravity.CENTER);
                       nom.setPadding(2, 2, 2, 2);
                       nom.setLayoutParams(layoutParams);

                       TextView prenom = new TextView(this);
                       prenom.setBackgroundColor(Color.LTGRAY);
                       prenom.setTextColor(Color.DKGRAY);
                       prenom.setGravity(Gravity.CENTER);
                       prenom.setPadding(2, 2, 2, 2);
                       prenom.setLayoutParams(layoutParams);

                       nom.setText("toto");
                       tr.addView(nom);

                       prenom.setText("AAA");
                       tr.addView(prenom);

                       tl.addView(tr, layoutParams);
               }
       }

Link to comment
Share on other sites

Tu as une erreur parce que tu insères les mêmes TextView/TableRow plusieurs fois dans des éléments différents. Il te faut instancier un TableRow et des TextView pour chaque ligne que tu insères

Ok j'ai compris mon erreur j'ai remodelé mon code

package com.application.projet;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class DetailsActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.details_layout);

	TableLayout tl = (TableLayout) findViewById(R.id.details_tl);
	TableRow tr;	

	LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
			LayoutParams.FILL_PARENT);
	layoutParams.setMargins(2, 2, 2, 2);

	for (int i = 0; i < 50; i++) {
		tr = new TableRow(this);
		tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		tr.addView(generateTextView("test", layoutParams));
		tr.addView(generateTextView(String.valueOf(i), layoutParams));
		tl.addView(tr, layoutParams);
	}
}

public TextView generateTextView(String texte, LayoutParams ly) {
	TextView result = new TextView(this);
	result.setBackgroundColor(Color.LTGRAY);
	result.setTextColor(Color.DKGRAY);
	result.setGravity(Gravity.CENTER);
	result.setPadding(2, 2, 2, 2);
	result.setText(texte);
	result.setLayoutParams(ly);
	return result;
}
}

Tout fonctionne! Merci du coup de main ;)

A tout hasard, je cherche maintenant à justifier une TextView, forum et google n'ont rien donné...

Edited by Mc Flurry
Link to comment
Share on other sites

Qu'appelle-tu "justifier" ? Ce n'est pas déjà ce que tu fais avec "gravity" ?

Jusitifier dans le sens commun du traitement de texte, c'est à dire que les lignes soient remplies jusqu'au bout.

C'est dans une autre textView, pas celles du tableLayout.

Edited by Mc Flurry
Link to comment
Share on other sites

  • 11 months later...

Bonjour,

Je me permet de poster ici car j'essaye de faire exactement la même chose que dans se sujet j'ai bien recréer de nouveaux objets à chaque tour de boucle mais l'aply m'envoi toujours bouler au lancement auriez vous une piste ? Je précise que je n'ai même rien dans le logcat !

Je précise que je fait pour du android 2.1 !

Edited by micka2589
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...