How to store different ids for EditTexts with the same name?

0

I have an interaction and inside it I create several EditText dynamically, but how do I keep the id for each EditText ?

My code :

for (int j = 0; j < vet.length; j++) {
    EditText nome = new EditText(this);
    nome.setId(0+x);
    EditText sobrenome = new EditText(this);
    sobrenome.setId(1+x);

    x = x+2;
}

I tried this logic, but I realized that its the size of my vector is 3 for example, it creates 3 pairs of EditText (name + surname), but the 3 pairs have the same id, which makes sense, and I needed to know if I clicked on the 1st, 2nd, or 3rd names.

Even with onClick() I can not differentiate the "names". Note: I'm putting all these EditTexts in a LinearLayout , if it was in a ListView I could get the position at least.

Do you know any Android method to achieve this? Thank you in advance.

    
asked by anonymous 07.08.2015 / 16:50

2 answers

3

What's missing is the EditText's , after being created, added to LinearLayout :

int x = 1;
for (int j = 0; j < vet.length; j++) {
    EditText nome = new EditText(this);
    nome.setId(x);
    adicionarAoLayout(nome);
    x++;
    EditText sobrenome = new EditText(this);
    sobrenome.setId(x);
    x++;
    adicionarAoLayout(sobrenome);
}

Where adicionarAoLayout() is the method that adds EditText's to its LinearLayout .

    
04.08.2016 / 15:45
0

try:

for (int j = 0, int x = 1; j < vet.length; j++, x += 2)
{
    EditText nome = new EditText(this);
    nome.setId(x);
    EditText sobrenome = new EditText(this);
    sobrenome.setId(x+1);
}
    
04.08.2016 / 10:39