0

Please explain how this for loop works:

  Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        for (EditText editText : editTexts) {
            editText.getText().toString();
            // whatever u want to do with the strings
        }
    }

Particularly, what does the for (EditText editText : editTexts) part do?

Reimius
  • 5,694
  • 5
  • 24
  • 42
kayveesin
  • 433
  • 1
  • 4
  • 17
  • 2
    Check this out: http://stackoverflow.com/questions/7763131/java-for-loop-syntax – Ken Wolf Jul 30 '13 at 17:21
  • 2
    `for (EditText editText : editTexts)` is basically the same as a `foreach` statement. It's saying (in English), for each item in `editTexts` we're going to create an `EditText` object called `editText`. Then you can use the methods from the `EditText` class on that object (i.e. `getText()`). – Mattiavelli Jul 30 '13 at 17:23

3 Answers3

2

This is saying for each EditText view within editTexts (an array or collection).

EXAMPLE
If editTexts was an array of editTexts like et1,et2,et3,et4 This loop would start at et1 accomlish all the work in the inner loop then go to et2 and do the same thing over and over until after et4 it will exit the loop.

ObieMD5
  • 2,684
  • 1
  • 16
  • 26
0

You should look at the type of 'editTexts'. It seems to be a simple 'foreach' loop in java.

Rémi F
  • 1,327
  • 12
  • 25
0

its for-each loop, EditText editText within array of editTexts

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71