繁体   English   中英

如何实时查询firebase中的所有数据?

[英]How to query all data in firebase realtime?

我想将文本与来自 firebase 的数据进行比较。 下面是我的代码,它只从 ing01 读取名称。 但实际上我希望它从所有成分数据中读取,而不仅仅是 ing01。 请帮帮我,谢谢。

数据库截图

               final String dataCompare=sb.toString();//.replaceAll("\\s+","");



                databaseReference = firebaseDatabase.getInstance().getReference().child("ingredients");//.child("ing01").child("iName");

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

                        for (DataSnapshot ds: dataSnapshot.getChildren()){

                            String iName = ds.child("iName").getValue(String.class);

                            if(dataCompare.equals(iName)){

                                mResultEt.setText(dataCompare);
                                mStatusEt.setText("OK");
                                mStatusEt.setTextColor(Color.GREEN);
                            }
                            else{
                                mResultEt.setText(dataCompare);
                                mStatusEt.setText("Not OK");
                                mStatusEt.setTextColor(Color.RED);
                            }
                        }

而不是firebaseDatabase.getInstance().getReference().child("ingredients").child("ing01").child("iName"); , 使用firebaseDatabase.getInstance().getReference().child("ingredients"); 获取所有成分

您可以使用第三方库,例如:Angularfire 这里有官方链接: https://github.com/angular/angularfire

final String dataCompare=sb.toString();
databaseReference = firebaseDatabase.getInstance().getReference().child("ingredients");

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String iName1 = dataSnapshot.child("ing01").child("iName");

        String iName2 = dataSnapshot.child("ing02").child("iName");

        //now here you can compare both the iName values with the dataCompare object.
    }
}

****编辑****

final String dataCompare=sb.toString();
Log.d("DataCompare: ", dataCompare);

databaseReference = firebaseDatabase.getInstance().getReference().child("ingredients");

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        if (dataSnapshot.getChildren() == null) {
            Log.d(TAG, "onDataChange: snapshot has no children");

        } else {
            int i = 1;
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String iName = ds.child("ing0" + i++).child("iName").getValue();

                // if this doesn't work try this -- 
                // String iName = ds.child("iName").getValue();

                Log.d(TAG, "name value: " + iName);
                if (iName.equals(dataCompare)) {
                    Log.d(TAG, "onDataChange: string equals");

                } else {
                    Log.d(TAG, "onDataChange: strings are not equal");
                }
            }
        }
    }
}

在我写的onDataChange()方法中:-

String iName = ds.child("ing0" + i++).child("iName").getValue();

在这里,我们正在读取当前位于“成分”的数据快照的子“ing01”(在循环的第一次迭代中),然后我们将“iName”及其值读取到iName字符串 object 中。

for each loop 将对dataSnapshot的所有子项执行此操作,这意味着您在 firebase 数据库中拥有的所有数据。

检查这个:

FirebaseDatabase.getInstance().getReference().child("ingredients").addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                   String iName = childSnapshot.child("iName").getValue(String.class);

                   //Implement your logic here
                }
            }

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

在这里,您将获得所有ingredients ,并通过迭代获得iName

但是根据你的实现逻辑。 获取所有成分是没有用的,您可以获取与compare查询匹配的特定成分。 尝试如下:

FirebaseDatabase.getInstance().getReference().child("ingredients").orderByChild("iName").equalTo(dataCompare).addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.hasChildren()) {
                    mResultEt.setText(dataCompare);
                    mStatusEt.setText("OK");
                    mStatusEt.setTextColor(Color.GREEN);
                } else {
                    mResultEt.setText(dataCompare);
                    mStatusEt.setText("Not OK");
                    mStatusEt.setTextColor(Color.RED);
                }
            }

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

暂无
暂无

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

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