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

Asked

Viewed 64 times

0

I have an interaction and within it create several EditText dynamically, but how to 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 realized that its the size of my vector is 3 for example, creates 3 pairs of EditText (name + surname), but the 3 pairs have the same id, which makes sense, and I needed to at least know if I clicked on the first name, 2nd or 3rd.

Nor with the onClick() I can differentiate the "names". Note: I am putting all these Edittexts in one LinearLayout, if it were in a ListView I could take the position at least.

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

  • 1

    I don’t understand when you say that the 3 pairs get the same id.

  • I expressed myself badly, it actually looks like this: name:4 surname:5; surname:4 surname:5; surname:4 surname:5;

  • By what I see in the code they should all be different. The only thing left is to initialize variable x: int x = 1;

  • I start out of for, but I think it gets the same id because Edittext has the same name, so even those that have already been created get the value of the last id.

  • I really needed to differentiate the names in some way. In Listview a simple getItem(Position) would work.

  • Where are they added to Linearlayout?

  • I’m playing inside a single Linearlayout that sits inside a scrollView, very simple structure.

Show 2 more comments

2 answers

3

What’s missing are the Edittext’s, after created, be 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 the Edittext’s to his Linearlayout.

0

trial :

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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.