c'moi Posted May 26, 2011 Share Posted May 26, 2011 création d'un tableau dynamique Message » Jeu 26 Mai 2011 15:36 Bonjour à tous, Comme mentionné dans le sujet,je veux créer un tableau dynamique, c-à-d: créer une ligne,la remplir puis l'ajouter à mon tableau. Pour cela,j'ai fait mes recherches et j'ai trouvé pas mal de code,mais ça ne marche pas chez moi et je ne sais pas pourquoi :x à l’exécution, la ligne que j'ai ajouté à partir du code XML s'affiche,mes les autres lignes, celles ajoutées dynamiquement, ne s'affichent pas :? malgré que l'espace du tableau varie en fonction du nombre de ligne que j'ai initialisé à 50 (j'ai remarqué ça à partir du background du tableau) voici le code que je teste depuis tout à l'heure: <?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" > <TableLayout android:layout_height="wrap_content" android:id="@+id/tdyn" android:background="#F87907" android:layout_width="fill_parent"> <TableRow> <TextView android:id="@+id/info" android:text="test11" android:textColor="#000000" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/info" android:text="test12" android:textColor="#000000" android:layout_gravity="center_horizontal" /> </TableRow> </TableLayout> </LinearLayout> Et pour le code Java: public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TableLayout tl = (TableLayout) findViewById(R.id.tdyn); 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.setTextSize(20); result.setVisibility(View.VISIBLE); result.setLayoutParams(ly); return result; } } Pourriez vous m'aider à résoudre ce problème SVP ? Link to comment Share on other sites More sharing options...
Mc Flurry Posted May 27, 2011 Share Posted May 27, 2011 Salut, est ce que tu as fait ces import : import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TableRow.LayoutParams; Edit : haha j'avais pas vu mais c'est mon code :) Link to comment Share on other sites More sharing options...
c'moi Posted May 27, 2011 Author Share Posted May 27, 2011 Tout à fait Mc Flurry,je viens de trouver la solution. En fait j'aurais dû importer "import android.widget.TableRow.LayoutParams;" au lieu de "import android.widget.TableLayout.LayoutParams;" tout simplement ;) Merci pour la réponse et merci pour le code aussi ;) Link to comment Share on other sites More sharing options...
Mc Flurry Posted May 27, 2011 Share Posted May 27, 2011 De rien ;) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.