I am setting up a debit card screen where you can add your debit/credit card to your account. I have it set up all nice and dandy, but my issue comes with checking whether the card input is a duplicate or not.
The code is functioning, but the part where the card is added to the account happens before the card is flagged as a duplicate, and I am not sure why this is happening.
Here is the code that calls my checkDuplicate()
method and then proceeds to add the card to the account if everything is valid
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkDuplicates(cardForm.getCardNumber());
if(!isDuplicate) {
if (cardForm.isValid()) {
alertBuilder = new AlertDialog.Builder(AddDebits.this);
alertBuilder.setTitle("Please ensure your card information is correct");
alertBuilder.setMessage("Card number: " + cardForm.getCardNumber() + "\n" +
"Card expiry date: " + cardForm.getExpirationDateEditText().getText().toString() + "\n" +
"Card CVV: " + cardForm.getCvv() + "\n" +
"Postal code: " + cardForm.getPostalCode() + "\n" +
"Name: " + cardForm.getCardholderName());
alertBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final CreateCard newCard = new CreateCard(cardForm.getCardholderName(), cardForm.getCardNumber(),
cardForm.getExpirationDateEditText().getText().toString(), cardForm.getCvv(), cardForm.getPostalCode());
amountOfCards(new AmountOfCardsCallback() {
@Override
public void onCallback(long amount, long defaultCard) {
userRef.child("amountOfCards").setValue(++amount);
userRef.child("Card" + amount).setValue(newCard);
}
});
dialogInterface.dismiss();
Toast.makeText(AddDebits.this, "Your card has been added to your account", Toast.LENGTH_LONG).show();
}
});
alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
The snippet of this I am most concerned about is the first few lines
public void onClick(View view) {
checkDuplicates(cardForm.getCardNumber());
if(!isDuplicate) {
if (cardForm.isValid()) {
The checkDuplicate()
method is shown below
private void checkDuplicates(final String cardNumber)
{
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
amountOfCards(new AmountOfCardsCallback() {
@Override
public void onCallback(long amount, long defaultCard) {
for(int i = 0; i < amount; i++) {
if (dataSnapshot.child("Card" + i).child("cardNumber").getValue() != null) {
if (dataSnapshot.child("Card" + i).child("cardNumber").getValue().toString().equals(cardNumber)) {
isDuplicate = true;
alertBuilder.setTitle("Duplicate Card")
.setMessage("You already have this card saved");
alertBuilder.show();
alertBuilder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
}
}
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
This method functions perfectly fine. It finds the duplicates and displays a dialog box when this happens. But the issue is it does not happen until after the card is added to the account.
So the if(cardForm.isValid())
code happens before the duplicate is flagged.
Why could this be happening?