简体   繁体   English

从Firebase实时数据库中获取并比较键和值

[英]Fetch and Compare Key and Value from Firebase Realtime Database

I'm trying to make an app in which user store his Data. 我正在尝试制作一个用户存储其数据的应用程序。 I've used user shared preferences to save user login details but still for security purpose, i made an activity(Verify Passcode Activity) that ask user Passcode to allow access on the data. 我使用了用户共享首选项来保存用户登录详细信息,但出于安全目的,我进行了一项活动(验证密码活动),要求用户密码允许访问数据。 Now i'm able to save, fetch and delete data. 现在,我可以保存,获取和删除数据。 But in my Verify Passcode Activity, i just want to fetch that particular key and value where passcode is stored. 但是在我的验证密码活动中,我只想获取存储密码的特定键和值。 I don't want to fetch complete data in Verify Passcode Activity. 我不想在“验证密码活动”中获取完整的数据。

Below is the code which i used to save passcode to database:- 下面是我用来将密码保存到数据库的代码:-

databaseReference.child(uniqueID).setValue(passcode);

Below is the code i'm tried to fetch only passcode:- 以下是我尝试仅获取密码的代码:-

dbreference.child(uniqueID).orderByChild("passcode").equalTo(pass).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            Toast.makeText(getApplicationContext(), "Data exist", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Data doesn't exist", Toast.LENGTH_LONG).show();
        }
    }

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

    }
});

Here uniqueID is "abhishek88666kumar" and pass is a variable used for getText() . 这里的uniqueID"abhishek88666kumar"pass是用于getText()的变量。

Here is the screenshot of my Firebase Database :- 这是我的Firebase数据库的屏幕截图:-

Database Screenshot 数据库截图

Here is Verify Passcode Activity Screenshot:- 这是验证密码活动截图:-

验证密码活动截图

Please have a look at Database Screenshot and Verify Passcode Activity Screenshot. 请看一下数据库截图和验证密码活动截图。

Agenda: 议程:

  1. getText() from textView inside activity. 来自活动内部textView的getText()。

  2. getValue from firebase realtime database (only for current user. 'abhishek88666kumar' in our case). 从Firebase实时数据库中获取getValue(仅适用于当前用户。在本例中为'abhishek88666kumar')。

  3. compare if 1 and 2 are same or not. 比较1和2是否相同。 (If same then redirect to MainActivity else show error.) (如果相同,则重定向到MainActivity,否则显示错误。)

Please tell me what i'm doing wrong. 请告诉我我在做什么错。

Assuming pass is a value like 1234 , this line: 假设pass是一个类似1234的值,则此行:

dbreference.child(uniqueID).orderByChild("passcode").equalTo(pass).addListenerForSingleValueEvent(...

Should be: 应该:

FirebaseDatabase.getInstance().getReference("passcodes").orderByValue().equalTo(pass).addListenerForSingleValueEvent(...

This way the DataSnapshot will contain all child nodes of passcodes that have the value 1234 , ie "abhishek88666kumar" in your JSON sample. 这样, DataSnapshot将包含passcodes值为1234所有子节点,即JSON示例中的"abhishek88666kumar"

Also be sure to study the documentation on sorting and filtering data . 另外,一定要研究有关数据排序和过滤文档

This is the first way to compare firebase data. 这是比较Firebase数据的第一种方法。

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passcode = editText.getText().toString();

                Toast.makeText(getApplicationContext(), "Data :"+passcode, Toast.LENGTH_LONG).show();

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference("passcode");
               Query applesQuery = ref.orderByValue().equalTo(passcode);

                applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {


                        if (dataSnapshot.exists()){

                            Toast.makeText(getApplicationContext(), "Data exist", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(getApplicationContext(), "Data doesn't exist"+passcode, Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        // Log.e(TAG, "onCancelled", databaseError.toException());
                    }
                });

            }
        });

This is the second way to compare firebase data. 这是比较Firebase数据的第二种方法。

btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            check = editText.getText().toString();

            Toast.makeText(getApplicationContext(), "Data :"+check, Toast.LENGTH_LONG).show();

            DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("passcode");
           // Query applesQuery = ref.child("Customers").orderByChild("abhishek88666kumar").equalTo(passcode);

            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
                    if(map.get("abhishek88666kumar")!=null){
                        passcode = map.get("abhishek88666kumar").toString();

                        if (passcode.equals(check))
                        {
                            Toast.makeText(getApplicationContext(), "Data exist", Toast.LENGTH_LONG).show();
                        }
                        else{


                            Toast.makeText(getApplicationContext(), "Data doesn't exist"+check, Toast.LENGTH_LONG).show();
                        }

                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    // Log.e(TAG, "onCancelled", databaseError.toException());
                }
            });

        }
    });

Both are working. 两者都在工作。 Hope it's help full for you. 希望对您有帮助。 thank you. 谢谢。

Thanks to all for your help :) Your answer didn't worked for me but with the help of your answers and my few knowledge, I made a solution for my problem myself. 感谢大家的帮助:)您的答案对我没有用,但是借助您的答案和我的一些知识,我自己为问题解决了。 Here is the code that matches my requirement. 这是符合我要求的代码。 This code is working exactly how I wanted. 这段代码完全按照我的要求工作。

dbreference = FirebaseDatabase.getInstance().getReference("passcodes");
                    Query query = dbreference.orderByKey().equalTo(currentUser); //current user is "abhishek88666kumar" in this case.
                    query.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if (dataSnapshot.exists()){
                                String passcode = txtPasscode.getText().toString;//Passcode is 1234 in this case.
                                String fetchedValue = "";
                                for (DataSnapshot snap : dataSnapshot.getChildren()){
                                    fetchedValue = (String) snap.getValue();
                                }
                                if (!passcode.equals(fetchedValue)){
                                    Toast.makeText(getApplicationContext(), "Passcode doesn't match.", Toast.LENGTH_LONG).show();
                                }else{
                                    Toast.makeText(getApplicationContext(), "Passcode matched.", Toast.LENGTH_LONG).show();
                                }
                            }else{
                                Toast.makeText(getApplicationContext(), "No passcode found for this user", Toast.LENGTH_LONG).show();
                            }
                        }

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

                        }
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM