在Firebase实时数据库中循环以获取仅一个值以进行匹配

我有这个应用程序,用于注册患者的个人信息,并将其作为Json格式存储在数据库中,格式如下:

Patients {
            patient id e.g.123456789 {
                                       name:
                                       email:
                                       password:
                                       bloodPressureReading {

                                               10:37:13AM29Oct-2019:{
                                                                     dia:34
                                                                     prp:45 
                                                                     sys:12
                                                                     }
                                                              }
                                     }


          }

我正在登录页面上,我需要用户输入用户名和密码来完成简单的登录过程。但是,我不知道如何从Firebase实时数据库中检索密码值。

P.S。我不想使用Firebase身份验证服务

我尝试了这段代码,但是没有用:

databasePatients.child("\"Patients\"").addValueEventListener(new ValueEventListener() {
                                                             @Override
                                                             public void onDataChange(Datasnapshot datasnapshot) {

                                                                 //showData(datasnapshot);

                                                                 //old Code
                                                                 /*
                                                                 for(Datasnapshot uniqueKeysnapshot : datasnapshot.getchildren()){
                                                                     //Loop 1 to go through all the child nodes of users
                                                                     for(Datasnapshot bookssnapshot : uniqueKeysnapshot.child("123456789").getchildren()){
                                                                         //loop 2 to go through all the child nodes of books node
                                                                         //String bookskey = bookssnapshot.getKey();
                                                                         //String password = bookssnapshot.getvalue();
                                                                     }
                                                                 }
                                                             }*/

我也尝试过此代码,尽管ID和密码正确,它也不会返回任何结果

按钮testbutton =(按钮)findViewById(R.id.button);

    // To move to register page
    testbutton.setOnClicklistener(new View.OnClicklistener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty( (testid.getText().toString().trim()))) {
                Toast.makeText(getapplicationContext(),"Enter id to search for!",Toast.LENGTH_SHORT).show();
                return;
            }else
            {
               Query query = databasePatients.child("Patients").orderByChild("id").equalTo(testid.getText().toString().trim());
                //Query query = databasePatients.child("Patients").child("123456789").orderByChild("id").equalTo(testid.getText().toString().trim());
                        query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(Datasnapshot datasnapshot) {
                    //Context context = getcontext();
                    if (datasnapshot.exists()) {
                        // datasnapshot is the "issue" node with all children with id 0

                        for (Datasnapshot user : datasnapshot.getchildren()) {
                            // do something with the individual "issues"
                            patients usersBean = user.getvalue(patients.class);

                            if (usersBean.password.equals(txvPassword.getText().toString().trim())) {
                                Intent intent = new Intent(getapplicationContext(),Mainactivity.class);
                                startactivity(intent);
                            } else {
                                Toast.makeText(getapplicationContext(),"Password is wrong",Toast.LENGTH_LONG).show();
                            }
                        }
                    } else {
                        Toast.makeText(getapplicationContext(),"User not found",Toast.LENGTH_LONG).show();
                    }
                }

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

                                                                 }
                                                             });

我也在 Login using email stored in firebase realtime database  ,但我不知道为什么(datasnapshot.exists())返回false。

任何帮助将不胜感激。

jian199 回答:在Firebase实时数据库中循环以获取仅一个值以进行匹配

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3162441.html

大家都在问