if语句不返回从firebase中检索数据的正确元素 [英] if statement not returning the correct elements on retrieving data from firebase

查看:131
本文介绍了if语句不返回从firebase中检索数据的正确元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 FirebaseDatabase 中有一些数据,如下所示:

  app 
-child1
-uniqueId1
-pId1
-lId1
-uniqueId2
-pId2
-lId2
-uniqueId3
-pId3
-lId3
-uniqueId4
-pId4
-lId4
-uniqueId5
-pId5
-lId5
-uniqueId6
-pId6
-lId6
-child2
-uniqueIdA1
-uniqueId7
-uniqueId8
-uniqueId9
-uniqueId10
-uniqueId11
-uniqueId1
-uniqueId2
-uniqueId3
-uniqueId4
-uniqueId5

我正在检索 child1 的数据,如下所示:

  public void fMethod(final String fID,final String blackListedId){
mDatabase.child(child1)。addChildEventListener(new ChildEventListener(){
@Override
public vo id onChildAdded(DataSnapshot dataSnapshot,String s){
if(dataSnapshot.getValue()!= null){
Profile profile = dataSnapshot.getValue(Profile.class);
String pID = profile.getPID();
String lID = profile.getLID();如果(pID.trim()。equals(AccessToken.getCurrentAccessToken()。getUserId()。trim())){
if(pID.trim()。equals(fID.trim() )){
if(!lID.trim()。equals(blackListedId.trim())){
//用满足条件/要求的元素填充视图

字符串listingID = profile.getlID();
Log.d(LISTING_IDS,listingID);

} else {
Log.d(dataSnapshot,null1);
}
} else {
Log.d(dataSnapshot,null2);
}
} else {
Log.d(dataSnapshot,null3);
}
} else {
Log.d(dataSnapshot,null4);


...
...
...
}

child2 的数据如下:

<$ ();} $ {
$ b $ db
mDatabase.child(child2)。child(AccessToken.getCurrentAccessToken()。getUserId())。addListenerForSingleValueEvent (DataSnapshot childSnapshot:dataSnapshot()){$ b $ if(dataSnapshot.getValue()!= null){
(new DataEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot) .getChildren()){
String blackListedId = childSnapshot.getValue()。toString();
fMethod(fID,blackListedId);
}
} else {
fMethod(fID,);
}
}

@Override
public void onCancelled(DatabaseError databaseError){


});

$ / code>

然后在另一个代码中检索 fID s并调用 fData()方法。



我记录了所有的id来自数据库:

  D / LISTING_IDS:uniqueId1 
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId1
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId2
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS :uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId3
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId4
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5
D / LISTING_IDS:uniqueId5

这是 Profile.java 文件代码: https://gist.github.com/HammadNasir/a196bcdc6dccbf69657fca528443e680



问题是在 fMethod()的if语句的条件是!lID.trim()。equals(blackListedId.trim )所以,你可以在数据库中看到,我应该得到 child1 uniqueId c>除了 uniqueId3 uniqueId7 ,因为这两个出现在 child2 也是我得到的所有 uniqueId s,除了 uniqueId3 uniqueId7 两次, uniqueId3 uniqueId7 一次。另外要注意的是,当我把这个条件定义为 lID.trim()。equals(blackListedId.trim())时, code>,我只得到了符合这个要求的2个id,即 uniqueId3 uniqueId7 如果 child2 uniqueId11 下只有一个id,那么我得到所有的 uniqueId s除了在这里有一个,但有2个或更多的id是造成这个问题。

我希望你有我的问题。

为什么!lID.trim()。equals(blackListedId.trim()正在返回意外的id,我怎么才能得到符合这个条件的id?

解决方案

addListenerForSingleValueEvent for child 2将永远被调用两次 - 一次当你设置它,第二次当它读取所有的数据,所以当它被称为第一次,它结束了调用 fMethod(fID,)这是你从哪儿得到了孩子1的所有ID,包括3和7,但下一次调用它的时候,你描述的就没有问题。所以如果你从你的child2 ValueEventListener 中删除​​else条件,我认为它应该可以正常工作。



让我知道,如果我理解你的问题,并回答它。如果没有,请随时解释更多细节。


I have some data in FirebaseDatabase which looks like this:

app
 -child1
   -uniqueId1
     -pId1
     -lId1
   -uniqueId2
     -pId2
     -lId2
   -uniqueId3
     -pId3
     -lId3
   -uniqueId4
     -pId4
     -lId4
   -uniqueId5
     -pId5
     -lId5
   -uniqueId6
     -pId6
     -lId6
 -child2
   -uniqueIdA1
     -uniqueId7
     -uniqueId8
     -uniqueId9
     -uniqueId10
     -uniqueId11
     -uniqueId1
     -uniqueId2
     -uniqueId3
     -uniqueId4
     -uniqueId5

I'm retrieving child1's data like this:

public void fMethod(final String fID, final String blackListedId) {
        mDatabase.child("child1").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.getValue() != null) {
                    Profile profile = dataSnapshot.getValue(Profile.class);
                    String pID = profile.getPID();
                    String lID = profile.getLID();
                    if (!pID.trim().equals(AccessToken.getCurrentAccessToken().getUserId().trim())) {
                        if (pID.trim().equals(fID.trim())) {
                            if (!lID.trim().equals(blackListedId.trim())) {
                            // populate the view with elements which meet this condition/requirement

                            String listingID = profile.getlID();
                            Log.d("LISTING_IDS", listingID);

                            } else {
                                Log.d("dataSnapshot", "null1");
                            }
                        } else {
                            Log.d("dataSnapshot", "null2");
                        }
                    } else {
                        Log.d("dataSnapshot", "null3");
                    }
                } else {
                    Log.d("dataSnapshot", "null4");
                }
            }
            ...
            ...
            ...
    }

and child2's data like this:

public void fData(final String fID) {

        mDatabase.child("child2").child(AccessToken.getCurrentAccessToken().getUserId()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue() != null) {
                    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                        String blackListedId = childSnapshot.getValue().toString();
                        fMethod(fID, blackListedId);
                    }
                } else {
                    fMethod(fID, "");
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

and then in another code I'm retrieving fIDs and calling fData() method there.

I logged all the ids I'm getting from the database:

D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5

Here's Profile.java file's code: https://gist.github.com/HammadNasir/a196bcdc6dccbf69657fca528443e680

The problem is that in fMethod()'s if statement the condition is !lID.trim().equals(blackListedId.trim() so, as you can see in database, I should get all the uniqueIds under child1 except uniqueId3 and uniqueId7 because these 2 are present in child2 too but instead I'm getting all the uniqueIds except uniqueId3 and uniqueId7 twice and uniqueId3 and uniqueId7 once.

Another thing to note is that when I'm making that condition as lID.trim().equals(blackListedId.trim(), I'm getting only the 2 ids which match this requirement, i.e, uniqueId3 and uniqueId7 and if child2 has only 1 id under uniqueId11 then I'm getting all the uniqueIds except the one in here but having 2 or more ids is causing the problem.

I hope you got my problem. I tried my best to explain it with the least possible code.

Why !lID.trim().equals(blackListedId.trim() is returning unexpected ids and how can I get only the ids which meet this condition?

解决方案

The addListenerForSingleValueEvent for child 2 will always be called twice - once when you set it up and second when it has read all the data. So when it is called the first time, it ends up calling fMethod(fID, "") which is where you are getting all IDs from Child 1 including 3 and 7. But next time when it calls it, it is doing fine as you describe it. So if you remove the "else" condition from your child2 ValueEventListener, I think it should work fine.

Let me know if I understood your question and answered it. If not, feel free to explain more details.

这篇关于if语句不返回从firebase中检索数据的正确元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆