Firebase监听器跳过

时间:2019-03-08 14:03:31

标签: android firebase firebase-realtime-database

我已转换为Firebase进行Android开发,因为我想利用Firebase的许多功能(通知,消息传递,存储等),但是,我很难从Firebase接收数据,也无法在线找到详细信息。是的,我已经尝试过该文档,但是没有解释我遇到的事件。

我想检索将其插入对象数组列表中的所有数据。这是一个静态示例:

users[i] = new User("James" + (i*2.5), "Hmack00" + (i*2), "https://firebasPic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=cf", "United Kingdom"); 

数据库:

   {
      "Users" : {
        "4PdlTlv3qjZ3BmDvrJyUut9Fnq43" : {
          "Country" : "xx",
          "Fullname" : "hh",
          "ProfilePicture" : "htm/o/Images%2FSearchAdapter%2F4PdlTlv3qjZ3BmDvrJyUut9Fnq43%2FProfilePicture%2FProfilePic_2019.03.06.10.47.54.jpg.jpg?alt=media&token=b647708e-c6d5-4b45-bef0-3dc40301b73a",
          "Username" : "hmack001"
        },
        "COg4r4io9hezhFpmK3adPucUXA93" : {
          "Country" : "spain",
          "Fullname" : "nat",
          "ProfilePicture" : "hcom/o/Images%2FSearchAdapter%2FCOg4r4io9hezhFpmK3adPucUXA93%2FProfilePicture%2FProfilePic_2019.03.06.19.14.17.jpg.jpg?alt=media&token=8620b321-5cef-42f0-a828-dbb7c37c8e7d",
          "Username" : "nat"
        },
        "Tw1xRxViygNsLqrQiaaMAvAduIu1" : {
          "Country" : "uk",
          "Fullname" : "harvey\n",
          "ProfilePicture" : "t.com/o/Images%2FUsers%2FTw1xRxViygNsLqrQiaaMAvAduIu1%2FProfilePicture%2FProfilePic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=c290e75a-5f92-4271-bcb5-c644fe1b14ef",
          "Username" : "RGB"
        },
        "vOxr1RoDqgWogKK1lp9pfpTHc6w2" : {
          "Country" : "scotland ",
          "Fullname" : "greg greg",
          "ProfilePicture" : "ot.com/o/Images%2FSearchAdapter%2FvOxr1RoDqgWogKK1lp9pfpTHc6w2%2FProfilePicture%2FProfilePic_2019.03.04.12.30.22.jpg.jpg?alt=media&token=27b024cf-0691-4121-8a27-26acf101ebc2",
          "Username" : "greg"
        },
        "xecUOPeyMcQaQrgkU9ouDgK90Ai1" : {
          "Country" : "ggh",
          "Fullname" : "Da apply ",
          "ProfilePicture" : "2FProfilePic_2019.03.03.04.58.50.jpg.jpg?alt=media&token=f35854c2-3ff9-4d18-9f7a-10c13f066c68",
          "Username" : "gg"
        }
      }
    }

这是我的代码,之后我将解释我的错误(我留在“僵尸”代码中,以显示我的尝试)

//Firebase Variables
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;

//Firebase Data
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    userId = mAuth.getCurrentUser().getUid();
    myRef = mFirebaseDatabase.getReference().child("Users").child(userId);

        //Firebase Data
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) { //Function called every time a change is made to the database
            showData(dataSnapshot);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) { //Function called when there is an error in the database/ with the call
            Log.d("Print","Cancelled Firebase: " + databaseError.toString());
        }
    });
}

private void showData(DataSnapshot dataSnapshot) {
    int userCount = 5;
    int i = 0;
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        //User user = new User();
        //User user = new User(); //Get Count of Firebase Users
        //user = new User();
        if (ds.child(userId).exists()) {
            /*user.setFullname(ds.child(userId).getValue(User.class).getFullname()); //set the full name
            user.setUsername(ds.child(userId).getValue(User.class).getUsername()); //set the username
            user.setProfilePicture(ds.child(userId).getValue(User.class).getProfilePicture()); //set the profile picture

            //Display Information
            Log.d("DataSnapchat Test", "ShowData Name: " + user.getFullname());
            Log.d("DataSnapchat Test", "ShowData Username: " + user.getUsername());
            Log.d("DataSnapchat Test", "ShowData Picture: " + user.getProfilePicture());

            ArrayList<String> userArrayList = new ArrayList<>();
            userArrayList.add(user.getFullname());
            userArrayList.add(user.getUsername());
            userArrayList.add(user.getProfilePicture());
            */

            String fullname = (String) ds.child(userId).child("Fullname").getValue();

            Toast.makeText(this, "Fullname: " + fullname, Toast.LENGTH_SHORT).show();

            //UserListAdapter adapter = new UserListAdapter(this, R.layout.find_profiles_search, userArrayList, mProfileSearch);
            //mListView.setAdapter(adapter);

            i++;
        }
    }
}

当我调试代码时,从不调用showData函数,也不调用Value Event Listener中的任何函数,这是异步问题吗?

目前,我正在尝试获取数据并将其插入变量中(实际上,任何数据,只要我有一个有效的查询,就可以通过处理子字段等将其转换为适合的数据。)

问题:addValueListener仅在数据库中的数据更改时才起作用吗?如果是这样,那有什么选择,如果不是,那么为什么这些功能不起作用。

我没有收到任何错误,也没有任何内容记录到控制台。

我想将数据库放入数组列表中,我知道我不应该使用getChildren来执行此操作,而在尝试获取所有数据之前,我要测试是否可以获取任何数据。

1 个答案:

答案 0 :(得分:0)

showData()中确实有一些怪异的循环。由于您要将ValueEventListener附加到特定用户的节点上,因此只需查找该特定用户的属性值即可:

myRef = mFirebaseDatabase.getReference().child("Users").child(userId);

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.d("OnDataChange", dataSnapshot.getKey());
        Log.d("OnDataChange", dataSnapshot.child("Fullname").getValue(String.class));
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("Print","Cancelled Firebase: " + databaseError.toString());
    }
});