1

I'm working on Eclipse ADT. I came across task where I need to check a number of EditText fields, so I decided I could use FOR loop to check all of them with less coding.

For instance I have:

EditText editTxt1 = (EditText) findViewById(R.id.editText1);
EditText editTxt2 = (EditText) findViewById(R.id.editText2);
EditText editTxt3 = (EditText) findViewById(R.id.editText3);
EditText editTxt4 = (EditText) findViewById(R.id.editText4);
...

and I want to check them with for loop like so:

int x;

for (x=3; x<=4; x++) {
    if (editTxt"x".getText().toString().equals("something")) do smthng...
}

Is it possible at all, or I have to go through long way coding?

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Armand
  • 2,611
  • 2
  • 24
  • 39

3 Answers3

1

You could do this via reflection as Quoi points out, but this is generally not a good a idea.

It would be better just to add your objects to a list or a map:

List<EditText> list = new ArrayList<EditText>();
EditText editTxt1 = (EditText) findViewById(R.id.editText1);
list.add(editTxt1);
EditText editTxt2 = (EditText) findViewById(R.id.editText2);
list.add(editTxt2);
EditText editTxt3 = (EditText) findViewById(R.id.editText3);
list.add(editTxt1);
EditText editTxt4 = (EditText) findViewById(R.id.editText4);

Now we can cycle through the list and use the index to check which edit text was being called.

int i = 0;
for (EditText e : list) {
    if(e.getText().toString().equals("something")) {
        System.out.println("editText" + i + " equals something");
        // do stuff
    }
    i++;
}

You could also use a Map to do this, this would allow you to have a name value against your objects to help give you a better reference to check what object was being called. This takes a bit more work, but might be use

Map<EditText, String> map = new HashMap<EditText, String>();
EditText editTxt1 = (EditText) findViewById(R.id.editText1);
map.put(editTxt1, "editTxt1");
EditText editTxt2 = (EditText) findViewById(R.id.editText2);
list.add(editTxt2, "editTxt2");

for (Entry<String, String> e : map.entrySet()) {
    if(e.getValue().equals("something")) {
        System.out.println(e.getKey() + " was equal to somethihng");
    }
}

PS - Never use == to compare Strings, it won't always work!!

Community
  • 1
  • 1
Tom
  • 15,798
  • 4
  • 37
  • 48
1

You must use the function getIdentifier(); something like:

int id = getResources().getIdentifier("editText" + x, "id", this.getPackageName());
EditText editTxt = (EditText) findViewById(id);

if (editTxt.getText().toString().equals("something")) do smthng...

Also, using something like editTxt instead of editText is not a good idea: for the saving of only one character, you are adding a lot of possible confusion in your code.

SylvainL
  • 3,926
  • 3
  • 20
  • 24
0

I was inspired by Anton's Kovalenko comment, to look for solution that he suggested. In my opinion this way is cleaner and less coding.

I'm sharing it to addition to other solutions listed here.

EditText[] editTextArr;
editTextArr = new EditText[4];

editTextArr[0] = (EditText) findViewById(R.id.editText1);
editTextArr[1] = (EditText) findViewById(R.id.editText2);
editTextArr[2] = (EditText) findViewById(R.id.editText3);
editTextArr[3] = (EditText) findViewById(R.id.editText4);

and now we can check edittext fields with FOR loop like so:

int i;
for (i=0; i<12; i++) {
    if (editTextArr[i].getText().toString().equals("something")) do smthng....
}
Armand
  • 2,611
  • 2
  • 24
  • 39