0

How to set error in Edittext if mobile number already exists in the Firebase database. If the user enters the existed number the values gets updated so anyone can give existed number and change the data. I want to display Toast or error if mobile already exist.

enter image description here

 public void onClick(View v) {
        String location = actv.getText().toString();
        String dob = age.getText().toString();
        String name1 = name.getText().toString();
        String mobile1 = mobile.getText().toString();
        String blood1 = blood.getSelectedItem().toString();
        String combo = name1 + "," + mobile1 + "," + location + "," + dob;

        if (blood1.equals("") || mobile1.equals("") ||  location.equals("Tap to detect") || dob.equals("") ) {

            Toast.makeText(MainActivity.this, "Please fill all the details.", Toast.LENGTH_SHORT).show();
        } else {


            mFirebaseDatabase.child("users").child(blood1).child(mobile1).setValue(combo);

            Toast.makeText(MainActivity.this, "Donor Added", Toast.LENGTH_SHORT).show();
            Intent home = new Intent(MainActivity.this, Home.class);
            startActivity(home);
            finish();
        }
    }
});
DEXTOR_ANTONY
  • 300
  • 1
  • 12
  • See https://stackoverflow.com/questions/37910008/check-if-value-exists-in-firebase-db. If you're having trouble making that work, edit your question to include the code that you already tried. – Frank van Puffelen Aug 30 '19 at 23:32
  • @FrankvanPuffelen I have added a second duplicate as I saw that the OP has edited the question to add Java code. The first duplicate is in Javascript, so I hope is more appropriate. – Alex Mamo Aug 31 '19 at 09:22
  • Thanks Alex! Will we see you in Madrid in a few weeks? – Frank van Puffelen Aug 31 '19 at 13:54
  • I've updated the question. Resolution would be appreciated. @FrankvanPuffelen – DEXTOR_ANTONY Aug 31 '19 at 16:21
  • What happens if you run the code in a debugger and put a breakpoint on `if (blood1.equals("")...`? What are the values of `blood1`, `mobile1` and `location`? Then where does the debugger go if you step through the code from there? – Frank van Puffelen Aug 31 '19 at 18:45
  • @FrankvanPuffelen It goes to mobile1 and so on as in the picture updated above. I'm new to android what should i do now. i just want to check mobile number already existed in db. thanks – DEXTOR_ANTONY Sep 01 '19 at 04:10
  • None of the code that is currently in your questions *reads* anything from the database, so it can't be checking for duplicates. The questions Alex and I linked show how to check for duplicates. – Frank van Puffelen Sep 01 '19 at 14:16

0 Answers0