0

I am a beginner learning android studio I want to check if the entered name already exists in database , also I want the unique id to be replaced by the name which the user entered.

Firebase Realtime Database Image:

Code:

public class MainActivity extends AppCompatActivity {
EditText txtname;
Button button;
DatabaseReference reff;
Member member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtname=(EditText)findViewById(R.id.txtname);
        button=(Button)findViewById(R.id.button);
        member=new Member();
        reff= FirebaseDatabase.getInstance().getReference().child("Member");

        reff.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    if (data.child(String.valueOf(txtname)).exists()) {
                        Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();//if the name exists
                    }
                    else {
                        Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();//if the name doesnt exist
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }


        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                member.setName(txtname.getText().toString().trim());
                reff.push().setValue(member);
                Toast.makeText(MainActivity.this, "Inserting Data into database and checking", Toast.LENGTH_LONG).show();
            }
        });
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    Does this answer your question? [Checking if a particular value exists in the Firebase database](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database) – LoukasPap Jan 21 '21 at 15:36

1 Answers1

0

To use the name as the key of the node, instead of using a push ID, replace:

reff.push().setValue(member);

With:

reff.child(txtname.getText().toString().trim()).setValue(member);

With that change, you can check if a name is already in use with:

ref.child("isThisNameInUse").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            ... the user exists already
        }
        else {
            ... the user does not exist yet
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

Note that the topic of checking whether a user name exists has been covered quite a few times already, so I recommend checking out some of the other relevant questions here and here.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hello sir thanks for answering me . when i freshly start the app it shows login successfull because its checking the last time i entered that name. i want to implement this like a login screen that a user sends the name if the name exists then it can say login successful and if not it shows login failed. (i know that im inserting the data because i dont know how to check plz help and explain me . thanks) – Mayuresh Rawal Jan 21 '21 at 15:52
  • I'm not sure I understand what you mean. Did you try the code I gave in my answer, that shows both how to write the data in the format you want, and how to them read it back? – Frank van Puffelen Jan 21 '21 at 16:42
  • https://stackoverflow.com/questions/65833080/why-is-it-showing-login-successful-even-if-its-not-meeting-the-search-criteria – Mayuresh Rawal Jan 21 '21 at 17:55
  • How does your new post relate to this question? – Frank van Puffelen Jan 21 '21 at 19:44