0

I'm trying to make a registry system with Firebase. I want to make the person's username unique. I made some code here, and it didn't work as expected. Someone help me fix my mistake

this is my code:

        BirthdayFragment fragment =  new BirthdayFragment();
        Bundle bundle = this.getArguments();

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usernameInput = username.getText().toString();

                Query usernameQuery  = FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("username").equalTo(usernameInput);
                usernameQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.getChildrenCount() > 0) {
                            Toast.makeText(getContext(), "Esse nome de usuário já existe", Toast.LENGTH_SHORT).show();
                        } else {
                            //
                            bundle.putString("username", usernameInput);
                            fragment.setArguments(bundle);

                            getFragmentManager().beginTransaction().replace(R.id.register_fragments_container, fragment).commit();
                        }
                    }

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

                    }
                });
            }
        });

in database:

enter image description here

  • Does this answer your question? [Firebase android : make username unique](https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique) – Dharmaraj Jun 30 '21 at 18:17
  • 1
    I don't particularly know your use case but what you want to achieve can be achieved easily from Firestore Database. In firestore, each document is unique in the database, which can be used as your username. Also note, DON'T store passwords in the way you are doing right now. – s_o_m_m_y_e_e Jun 30 '21 at 18:21
  • In addition to Dharmaraj's link, this may also be of help: [Firebase Android Check for duplicate values](https://stackoverflow.com/questions/67819240) – samthecodingman Jun 30 '21 at 20:22

0 Answers0