数据未从Firebase数据库中检索到。

huangapple go评论69阅读模式
英文:

Data not retrieving from Firebase database

问题

[![在这里输入图片描述][1]][1]

我有一个如图所示的 Firebase 数据库我正在尝试根据一些条件检索数据但没有显示任何结果

DbRef2 = FirebaseDatabase.getInstance().getReference().child("Notification");

DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
            Notification notf = dataSnapshot.getValue(Notification.class);
            Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
        }
    }

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

    }
});

在吐司中没有显示任何内容我还尝试使用 equalTo 替代 startAt但没有结果
英文:

数据未从Firebase数据库中检索到。

I have a Firebase database as shown in the pic. I am trying to retrieve data based on some condition but it shows no results.

    DbRef2 =FirebaseDatabase.getInstance().getReference().child("Notification");

    DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
                Notification notf = dataSnapshot.getValue(Notification.class);
                Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
            }
        }

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

        }
    });

There is nothing shows in the toast. I also tried equalTo instead of startAt. but no results.

答案1

得分: 1

你的代码中有四个问题。第一个问题是你使用的查询。如果你想根据条件获取结果,请将以下查询更改为:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(/* ... */);

不需要使用 .child("45961"),因为你需要遍历结果。

第二个问题是,你将 DataSnapshot 对象作为参数,但没有循环来获取结果。要实际获取结果,应使用以下代码:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()){
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
            Notification notf = ds.getValue(Notification.class);
            Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
        }
    }
}

第三个问题是,你没有检查潜在的错误。你也可以考虑使用:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    Log.d("TAG", databaseError.getMessage()); //不要忽略潜在的错误!
}

第四个问题是你使用了以大写字母开头的名称。关于这点,我建议你查看以下帖子中的答案:

https://stackoverflow.com/questions/54069574/firebase-android-listview-not-being-displayed

或者:

https://stackoverflow.com/questions/53924189/how-to-read-firestore-sub-collection-and-pass-it-to-firestorerecycleroptions/53943559#53943559

如果你的类中的字段是小写的。

英文:

There are four issues in your code. The first one would be the query that you are using. If you want to get the results based on a condition, please change the following query:

DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(/* ... */);

To:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(/* ... */);

There is no need for a .child("45961") call, as you need to loop through the results.

The second issue is that you get the DataSnapshot object as an argument but you don't loop to get the results. To actually get the results you should use the following code:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()){
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
            Notification notf = ds.getValue(Notification.class);
            Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
        }
    }
}

The third issue is the fact that you are not checking for potential error. You also might consider using:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
	Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}

The fourth issue lies in the fact that you are using names that start with an uppercase. For that, I recommend you see my answer from the following post:

> https://stackoverflow.com/questions/54069574/firebase-android-listview-not-being-displayed

Or:

> https://stackoverflow.com/questions/53924189/how-to-read-firestore-sub-collection-and-pass-it-to-firestorerecycleroptions/53943559#53943559

If the fields in your class are lowercase.

答案2

得分: 0

将此更改为:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {

如果您想在orderByChild()内部使用FromId,则不需要访问child("45961")

英文:

Change this:

 DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {

into this:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {

If you want to use FromId inside orderByChild() then you don't need to access child("45961")

huangapple
  • 本文由 发表于 2020年9月14日 16:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63880943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定