Take programmatically generated Edittext values

Asked

Viewed 471 times

1

I have an APP, which preencho a quantidade de jogadores that will have the championship and I click on a botão to create the championship. This button plays for a activity that takes the amount that was set and gera N EditText with a loop. So far ok.

After you begat these EditText, I fill in all the names, and I need that when I click on botão, he go to another activity and take the valores of EditText.

To questão is: how I pull them valores of EditText, if they are generated programmatically and precisely ID for that reason ??

It would be throwing them into an array and then consulting !?

Example of what I use to generate EditText without the loop for now:

public class MainActivity extends AppCompatActivity {

    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.layout);

        EditText et = new EditText(this);
        et.setText("Jogador");
        ll.addView(et);
    }

2 answers

1

As you already have the amount of players, first create a ArrayList of EditText and put them in your layout:

public class MainActivity extends AppCompatActivity {

    private LinearLayout ll;
    private ArrayList<EditText> listaEdt = new ArrayList<EditText>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle b=this.getIntent().getExtras();
        int qntdJogadores = b.getStringArray("quantidade");

        ll = (LinearLayout) findViewById(R.id.layout);

        for (int i = 0; i < qntdJogadores; i++) {
            listaEdt.add(new EditText(this));
            listaEdt.get(i).setText("Jogador " + i);
            ll.addView(listaEdt.get(i));
        }
    }
}

Then do the following to take the values and move on to the next Activity:

ArrayList<String> nomes = new ArrayList<String>();

for (int i = 0; i < listaEdt.size(); i++) {
    nomes.add(listaEdt.get(i).getText().toString());
}

Bundle bundle = new Bundle();
bundle.putStringArrayListExtra("NOMES_ARRAY_LIST", nomes);
Intent i = new Intent(MainActivity.this, OutraActivity.class);
i.putExtras(bundle);
startActivity(i);

0

First of all you will need to store all your Edittext in one View. After you store in a view and then go counting your children, you should use the getChild method to be able to pick up the children from the View, and go storing the values of the children in an array:

final int childCount = viewLayout.getChildCount();
String[] str = new String[childCount];
for (int i = 0; i < childCount; i++) {
      EditText edt = viewLayout.getChildAt(i);
      childCount = edt.getText().toString();
}

With this you will store the values within Edittext in an array of strings.

To exchange data between activitys you will need to use Intent I will give you a small example to adapt it to your code.

Intent intent = new Intent(this, OutraActivity.class);
intent.putExtra("SEU_TEXTO", str);
startActivity(intent);

And in the other Activity you can pick up the value by the key SEU_TEXTO

String[] textos = getIntent().getStringArray("SEU_TEXTO");
  • Where would that enter my loop if I don’t have the right amount of Edittexts ?

  • That way when you click the button, you will pass the values of one acitity to another..

  • I understand better what you’re talking about...

  • Yes, but I have to do this for a varied amount of Edittext... as I know which names or ID ?

  • I reedited my answer from a glance

Browser other questions tagged

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