Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
63 views
in Technique[技术] by (71.8m points)

java - How to check if the name exists in firebase?

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();
            }
        });
    }
}
question from:https://stackoverflow.com/questions/65830647/how-to-check-if-the-name-exists-in-firebase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...