Reputation: 2509
I have one function who check if emails in Array
are correct or not. If emails are valid return true
and send emails to server.
My problem comes when emails are invalid. I put every email in ArrayList
and show them in AlertDialog
. But in AlerDialog
only one invalid email is shown because return false
.
I tried to move this return but everything comes wrong.
I want to go trough Array
with loop
and when loop
ends then it should return false
and show ArrayList
(I think this should do the job ), but when I move return false
after the loop
, AlertDialog
didn't work
This is my function
boolean checkedMails(String[] mails) {
for (int i = 0; i < mails.length; i++) {
if (!isEmailValid(mails[i])) {
wrongEmails.add(mails[i]);
Log.d("WRONGEMAILS", wrongEmails.toString());
return false;
}
}
return true;
}
And here i send emails or show alerDialog
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String IRegEmail = waitingForAdd.getText().toString();
String[] mails = getAnswers();
try {
if (checkedMails(mails)) {
sendEmails(mails);
} else {
alertDialogThis = new AlertDialog.Builder(
ReferFriend.this);
timer = new Timer();
alertDialogThis.setMessage("Wrong emails are:"
+ wrongEmails);
dlg = alertDialogThis.create();
dlg.show();
// dialog.dismiss();
timer.schedule(new TimerTask() {
public void run() {
dlg.dismiss();
timer.cancel();
}
}, 2000);
wrongEmails.clear();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Please add e-mails", Toast.LENGTH_LONG).show();
}
}
});
}
Upvotes: 1
Views: 124
Reputation: 885
you could just set a boolean variable before the loop, and return it when the check is done.
boolean checkedMails(String[] mails) {
boolean isGood = true;
for (int i = 0; i < mails.length; i++) {
if (!isEmailValid(mails[i])) {
wrongEmails.add(mails[i]);
Log.d("WRONGEMAILS", wrongEmails.toString());
isGood = false;
}
}
return isGood;
}
Upvotes: 1
Reputation: 151
Only 1 email is showing because after the first invalid email, the function returns. I think this should help, add an additional flag for keeping track if the whole set is valid/invalid.
boolean checkedMails(String[] mails) {
boolean isValid = true;
for (int i = 0; i < mails.length; i++) {
if (!isEmailValid(mails[i])) {
wrongEmails.add(mails[i]);
Log.d("WRONGEMAILS", wrongEmails.toString());
isValid = false;
}
}
return isValid;
}
Upvotes: 4