Reputation: 250
I have a dialog box that allowing the user to enter in values, each value gets validated to ensure the correct value has been entered. However on an invalid entry the values of the EditText are getting written to the SQLite database when the user clicks Cancel.
This is my code for the Dialog Box:
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton( AlertDialog.BUTTON_POSITIVE).setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show Toast Message When No Text Is Entered
//Validation Being Done Here
if (TextUtils.isEmpty(inputScore.getText().toString())) {
inputScore.setText( "0" );
//return;
}
if (TextUtils.isEmpty(inputStrikes.getText().toString())) {
inputStrikes.setText( "0" );
//return;
}
if (TextUtils.isEmpty(inputSpares.getText().toString())) {
inputSpares.setText( "0" );
//return;
}
if (TextUtils.isEmpty(inputSplits.getText().toString())) {
inputSplits.setText( "0" );
//return;
}
if (TextUtils.isEmpty(inputSplitConversions.getText().toString())) {
inputSplitConversions.setText( "0" );
//return;
}
if (TextUtils.isEmpty(inputOpenFrames.getText().toString())) {
inputOpenFrames.setText( "0" );
//return;
}
final int valueScore = Integer.valueOf(inputScore.getText().toString());
final int valueStrikes = Integer.valueOf(inputStrikes.getText().toString());
final int valueSpares = Integer.valueOf(inputSpares.getText().toString());
final int valueSplits = Integer.valueOf(inputSplits.getText().toString());
final int valueSplitConversions = Integer.valueOf(inputSplitConversions.getText().toString());
final int valueOpenFrame = Integer.valueOf(inputOpenFrames.getText().toString());
if (valueScore > 300) {
inputScore.setError("Maximum value allowed is 300");
//return;
} else
if (valueStrikes > 12) {
inputStrikes.setError("Maximum value allowed is 12");
//return;
} else
if (valueSpares > 10) {
inputSpares.setError("Maximum value allowed is 10");
//return;
} else
if (valueSplits > 10) {
inputSplits.setError("Maximum value allowed is 10");
//return;
} else
if (valueSplitConversions > 10) {
inputSplitConversions.setError("Maximum value allowed is 10");
//return;
} else
if (valueOpenFrame > 10) {
inputOpenFrames.setError("Maximum value allowed is 10");
//return;
} else {
alertDialog.dismiss();
}
//Check If User Is Updating Game
//Create New Game
if (shouldUpdate && game != null) {
//Update Game By It's Id
updateGame(inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString(), position);
} else {
createGame(leagueId.getText().toString(), bowlerId.getText().toString(), seriesId.getText().toString(), inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString());
}
seriesAverage = db.getSeriesAverage(savedLeagueId, savedBowlerId, savedSeriesId);
bowlerAverage = db.getBowlerAverage(savedLeagueId, savedBowlerId);
leagueAverage = db.getLeagueAverage(savedLeagueId);
}
});
}
I would like it to display all the fields that are incorrect at once and not save the values on a cancel. Can't seem to get this to happen though. Below is a image of what is currently happening.
Upvotes: 0
Views: 19
Reputation:
Try this code, with a boolean flag that indicates if all values are valid.
I include code for the cancel button just to close the dialog (I hope there are no typos).
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show Toast Message When No Text Is Entered
//Validation Being Done Here
Boolean allValid = true;
if (TextUtils.isEmpty(inputScore.getText().toString())) {
allValid = false;
inputScore.setText( "0" );
}
if (TextUtils.isEmpty(inputStrikes.getText().toString())) {
allValid = false;
inputStrikes.setText( "0" );
}
if (TextUtils.isEmpty(inputSpares.getText().toString())) {
allValid = false;
inputSpares.setText( "0" );
}
if (TextUtils.isEmpty(inputSplits.getText().toString())) {
allValid = false;
inputSplits.setText( "0" );
}
if (TextUtils.isEmpty(inputSplitConversions.getText().toString())) {
allValid = false;
inputSplitConversions.setText( "0" );
}
if (TextUtils.isEmpty(inputOpenFrames.getText().toString())) {
allValid = false;
inputOpenFrames.setText( "0" );
}
final int valueScore = Integer.valueOf(inputScore.getText().toString());
final int valueStrikes = Integer.valueOf(inputStrikes.getText().toString());
final int valueSpares = Integer.valueOf(inputSpares.getText().toString());
final int valueSplits = Integer.valueOf(inputSplits.getText().toString());
final int valueSplitConversions = Integer.valueOf(inputSplitConversions.getText().toString());
final int valueOpenFrame = Integer.valueOf(inputOpenFrames.getText().toString());
if (valueScore > 300) {
allValid = false;
inputScore.setError("Maximum value allowed is 300");
}
if (valueStrikes > 12) {
allValid = false;
inputStrikes.setError("Maximum value allowed is 12");
}
if (valueSpares > 10) {
allValid = false;
inputSpares.setError("Maximum value allowed is 10");
}
if (valueSplits > 10) {
allValid = false;
inputSplits.setError("Maximum value allowed is 10");
}
if (valueSplitConversions > 10) {
allValid = false;
inputSplitConversions.setError("Maximum value allowed is 10");
}
if (valueOpenFrame > 10) {
allValid = false;
inputOpenFrames.setError("Maximum value allowed is 10");
}
if (!allValid) {
return;
}
alertDialog.dismiss();
//Check If User Is Updating Game
//Create New Game
if (shouldUpdate && game != null) {
//Update Game By It's Id
updateGame(inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString(), position);
} else {
createGame(leagueId.getText().toString(), bowlerId.getText().toString(), seriesId.getText().toString(), inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString());
}
seriesAverage = db.getSeriesAverage(savedLeagueId, savedBowlerId, savedSeriesId);
bowlerAverage = db.getBowlerAverage(savedLeagueId, savedBowlerId);
leagueAverage = db.getLeagueAverage(savedLeagueId);
}
});
alertDialog.getButton( AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
Upvotes: 1