Pick programmatically generated EditText values

2

I have an APP, which preencho a quantidade de jogadores 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, okay.

After you have generated these EditText , I fill in all the names, and I need them when I click on botão , it will go to another activity and take the valores of EditText . >

The questão is: how do I pull the valores of the EditText , if they are programmatically generated and I need ID for this ??

Would you be throwing them into an array and then browsing !?

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);
    }
    
asked by anonymous 04.10.2017 / 20:44

2 answers

2

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 get the values and move 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);
    
04.10.2017 / 21:29
1

First of all you will need to store all of your EditText in a View. After you store in a view and then go counting your children, you should use the getChild method to get the children of the View, and store 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 that are within EditText in an array of strings.

To exchange data between activities 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 get the value by the key SEU_TEXTO

String[] textos = getIntent().getStringArray("SEU_TEXTO");
    
04.10.2017 / 20:49