Taylor Dawson Taylor Dawson - 3 months ago 8
Android Question

Method that checks if all edittexts are empty always return true

This may be something very simple that I have overlooked but I have this method here:

public boolean isEdited(){

for (int i = 0; i <= EDITTEXT_IDS.length; i++ ){
EditText edittext = (EditText) getActivity().findViewById(EDITTEXT_IDS[i]);
if(isEmpty(edittext)){
return true;
}
}
return false;
}

public boolean isEmpty(EditText myeditText) {
return myeditText.getText().toString().trim().length() == 0;
}


Which as you can see runs through a list of edittext boxes by id here:

private static final int[] EDITTEXT_IDS = {
R.id.entry_home,
R.id.entry_away
};


The method isEdited only has issues if I use an array. If I do them individually it works fine. Like so:

public boolean isEmpty(EditText myeditText) {
return myeditText.getText().toString().trim().length() == 0;
}
EditText editText = (EditText) getActivity().findViewById(R.id.entry_home);
isEmpty(editText);


It's probably something so minor.
I would really like the method to be like so:

public boolean isEdited(){

for ( id : EDITTEXT_IDS){
EditText edittext = (EditText) getActivity().findViewById(id);
if(isEmpty(edittext)){
return true;
}
}
return false;
}


Thank you in advanced!

Answer

Change:

public boolean isEmpty(EditText myeditText) {
    return myeditText.getText().toString().trim().length() == 0;
}

for this:

public boolean isEmpty(EditText myeditText) {
    return myeditText.getText().toString().trim().equals("");
}

But i think it's a good idea check if your EditTexts are been finding on the array, maybe you misspelled some of the names on the Array declaration