简体   繁体   English

如何在不知道孩子的情况下根据里面的价值获得孩子。 在 android firebase 实时数据库中

[英]How to get child based on value inside it, without knowing the child. in android firebase realtime database

I have a node for the pharmacies accounts and the children are the id of the pharmacies each child contain the name and the id of the pharmacy and some more info.我有一个药房帐户的节点,孩子们是药房的 ID,每个孩子都包含药房的名称和 ID 以及更多信息。 Now I have the pharmacy name and want to get the pharmacy ID is there any function that can do this?现在我有了药房名称并想获得药房 ID 是否有任何 function 可以做到这一点?

What you're looking for is a database query, which allows you to filter the results under a node based on a property value.您正在寻找的是数据库查询,它允许您根据属性值过滤节点下的结果。

In this case that'd be something like this:在这种情况下,这将是这样的:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Pharmacies");
Query query = ref.orderByChild("PharmacyName").equalTo("El Ezaby");
query.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (!task.isSuccessful()) {
            Log.e("firebase", "Error getting data", task.getException());
        }
        else {
            DataSnapshot result = task.getResult();
            for (DataSnapshot snapshot: result.getChildren()) {
                Log.i("firebase", snapshot.child("PharmacyID").getValue(String.class));
            }
        }
    }
});

The loop over result.getChildren() is needed since a query can have multiple results.由于查询可以有多个结果,因此需要对result.getChildren()进行循环。 Even though it may have only one result here, you'll still need the loop.即使这里可能只有一个结果,您仍然需要循环。

I have used the above code but there was an error at query.get我已经使用了上面的代码,但是在 query.get 中出现了错误

The code below worked with me下面的代码对我有用

 DatabaseReference databaseReference1=FirebaseDatabase.getInstance().getReference("Pharmacies");
     databaseReference1.addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(@NonNull DataSnapshot snapshot) {
           for(DataSnapshot dataSnapshot:snapshot.getChildren()){
               Pharmacies pharmacies=dataSnapshot.getValue(Pharmacies.class);
               if(pharmacies!=null){
                   if(pharmacyName.contains(pharmacies.getPharmacyName())){
                        Log.e("firebase", dataSnapshot.child("PharmacyID").getValue(String.class));
                        pharmacyid=dataSnapshot.child("PharmacyID").getValue(String.class);

                   }
               }
           }
         }

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

         }
     });

暂无
暂无

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

相关问题 在不知道 Firebase 实时数据库中的键值的情况下检索子值 - Retrieving child value without knowing key value from Firebase realtime database 如何根据子值从 Firebase 实时数据库中检索数据 - How to retrieve data from firebase realtime database based on child value 从孩子内部的实时 Firebase 获取价值 - Get Value From Realtime Firebase Inside Child 如何将值与 Firebase 实时数据库的子项进行比较? - How to compare value with child of Firebase Realtime Database? 如何在 Firebase 实时数据库(Android 开发)中获取特定子项的键值? - How to get the key value of a specific child in the Firebase Realtime database (Android Development)? 是否可以在按钮单击时从 android 更新 firebase 实时数据库中没有 ID 或键的特定孩子的值? - Is it possible to update a specific child's value without ID or key in firebase realtime database from android on button click? Android 和 Firebase 将 EditText 与实时数据库子值进行比较 - Android & Firebase comparing EditText to realtime database child value Firebase数据库如何基于子值获取节点? - Firebase database how to get nodes on based of the child value? 如何更新Firebase实时数据库中特定子节点的值? - How to update value of a particular child node in firebase realtime database? 如何在 Firebase 实时数据库中的特定子树中推送值 - How to push value in specific child tree in Firebase Realtime Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM