Comparing EditText string with firebase database's child value

不想你离开。 提交于 2021-01-28 05:08:37

问题


My app has a registration page wherein only those users who enter the right passcode are allowed to register. This passcode is stored inside firebase database inside a child called "Councilpasscodes". After entering the passcode the user should press the submit button, after comparing the two passcodes the user should be allowed to register if the passcodes are same. This is the code for comparing the edit text string and the child value:

mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes"); 
submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String passcode1=editText.getText().toString().trim();
                mpasscode.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String passc= (String) dataSnapshot.child("passcode").getValue();
                        if (passcode1.equals(passc)){
                            //do something


                        }else if(!passcode1.equals(passc)){
                            //do something

                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });

Database screenshot

The problem is after pressing the submit button it is not really checking the two strings i.e. if (passcode1.equals(passc)) and if(!passcode1.equals(passc)) is not being called even after entering the right passcode. I am not sure what is wrong with the code as I am new to app development.Any help is appreciated.Thank you.

EDIT: I think it is not executing addListenerForSingleValue at all. I am not sure why is this happening.


回答1:


Alright mate. The reason why your ValueEventListener's onDataChange isn't getting called is that you're not authorized to edit or read your database. This happens because the default security settings for a firebase real-time database disallows any read or write without an authenticated user. To prove this observe your warning logs inside Android Monitor. (The blue ones.) It will show a caught exception mentioning permission denied from Firebase as soon as you click submit.

Read about Firebase security from here.

For a quick solution what you can do is, you can allow read or writes without any sort of authentication. To do that edit your Realtime database rules and add this instead of what's written there.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

I'm also gonna attach a screenshot for reference. You'll find this screen inside the firebase console. Inside the console click on your project and then on Database to the left as shown.

.

This is a security lapse none the less, so to set up proper Authentication from your app, follow these steps. https://firebase.google.com/docs/auth/android/start/

Also, add a toString() to dataSnapshot.child("passcode").getValue() like this to convert it to String as it's probably long.

String passc = dataSnapshot.child("passcode").getValue().toString();

Also, just to be sure. Add this line if you haven't already FirebaseApp.initializeApp(this); above your mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");

Hope it helps!




回答2:


Have you tried using .compareTo() method? if (passcode1.compareTo(passc) == 0) { }




回答3:


I think firebase is returning integer instead. You can put the quote to 1234 and try again or compare as integer.




回答4:


if you are using only numerics then equalsIgnoreCase would do good or if there is no limit of type of character you use in counsilcode then .match would be good



来源:https://stackoverflow.com/questions/45734001/comparing-edittext-string-with-firebase-databases-child-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!