-2

i have several layout inflater in my code, which is necessary:

    for (String valori : timers_valori){
        LayoutInflater inflater=(LayoutInflater)getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View menuLayout = inflater.inflate(R.layout.elemento_statistica, contenitore_statistiche, true);
        TextView nome=(TextView)menuLayout.findViewById(R.id.timer_testo_1);
        nome.setText(x+"");
        menuLayout.invalidate();
        x=x+1;
    }

(the code is trimmed)

it works, but it continue to edit the first layout, for example:

OUTPUT ciao ... ...

INSTEAD I NEED hello hola ciao

any idea? i think it's not linkin correctly the textview.

thanks!

D Ferra
  • 1,223
  • 3
  • 12
  • 21

3 Answers3

0

It seems that you need to iterate the id R.id.timer_testo_1 on each loop, as it is overriding the content of same TextView. Another tip is to get LayoutInflater outside of the for.

Rafael Toledo
  • 5,599
  • 6
  • 23
  • 33
0

I think you should add each view that generates the main view,

Example:

addView (nome);

You should do this in every cycle of the loop.

Your code would look like: :

for (String valori : timers_valori){
    LayoutInflater inflater=(LayoutInflater)getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View menuLayout = inflater.inflate(R.layout.elemento_statistica, contenitore_statistiche, true);
    TextView nome=(TextView)menuLayout.findViewById(R.id.timer_testo_1);
    nome.setText(x+"");
    menuLayout.invalidate();
    x=x+1;
    addView(nome);
}
Samuel Liz
  • 49
  • 8
0

Solved by simply adding

nome.setId(x);

right after TextView declaration.

Pang
  • 9,564
  • 146
  • 81
  • 122
D Ferra
  • 1,223
  • 3
  • 12
  • 21