[Besoin d'aide] Prog Dynamique, boucles et id
Bonsoir,
je souhaites ajouter dynamiquement des éléments dans mon application jusque la pas trop de soucis mais je fais une boucle dans laquelle j'ajoute des composants dans mon relative layout en leur donnant un ID etc.. mais je n'arrive pas a trouver comment récupérer un textview crée dans un tour de boucle précédent.
Exemple:
Dans ma boucle au début j'ajoute un bouton avec un textview puis je créer un deuxième bouton et un deuxième textview.
J'aimerais accéder au premier textview mais je ne sais pas quoi utiliser. Je voudrais quand on clique sur le bouton Plus que le textview change.
Code:
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
|
int x = 20;
int y = 580;
int flag = 0;
int flagy = 0;
int flagx = 0;
int tmp = 0;
while(i < sTest.length)
{
//tvNb commence a 0
TextView tvNb= new TextView(this);
Button bPlus = new Button(this);
tvNb.setId(View.generateViewId());
bPlus.setId(View.generateViewId());
//lp = layoutparams
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(x, y, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvNb.setLayoutParams(lp);
tvNb.setTextSize(25);
tvNb.setText("0");
x += 110;
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(x, y, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
bPlus.setLayoutParams(lp);
bPlus.setText("+");
//rv = relative layout
rv.addView(tvNb);
rv.addView(bPlus);
i++;
bPlus.setOnClickListener(btnclick);
y += 150;
}
sv.addView(rv, rlp);
setContentView(sv);
View.OnClickListener btnclick = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int i = 0;
clicked = true;
Log.v("button ID = ", String.valueOf(v.getId()));
while (i < nbButtonP.length)
{
if (v.getId() == nbButtonP[i])
Toast.makeText(getApplicationContext(), "TEST" + String.valueOf(i), Toast.LENGTH_SHORT).show();
i++;
}
}
}); |
C'est une reconstitution de mon code, parce qu'il est pas beau ya plein de trucs inutile dans l'original dans celui ci aussi mais moins.
PS: il n'y a rien dans mon XML