为什么childEventListener不起作用?

时间:2018-08-31 13:44:38

标签: android firebase firebase-realtime-database event-listener

使用 // Code goes here var i; for (i = 0; i < 10; i++) { var btn = null; btn = document.createElement("BUTTON"); btn.setAttribute("name", i + "_name"); var t = document.createTextNode("Button" + i); btn.appendChild(t); document.body.appendChild(btn); if (btn.addEventListener) { btn.addEventListener('click', function() { alert("i am button " + this.getAttribute("name")); }); } else if (btn.attachEvent) { // IE < 9 :( btn.attachEvent('onclick', function() { alert("i am button " + this.getAttribute("name")); }); } }的代码如下:

    <!DOCTYPE html>
    <html>

      <head>
        <link rel="stylesheet" href="style.css">
        <script src="script.js"></script>
      </head>

      <body>
        <h1>Hello Plunker!</h1>
        <script src="script.js"></script>
      </body>

    </html>

childEventListener定义为:

DatabaseReference TUidRef = usersRef.child(td);
            Log.i("hello",td);

            TUidRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                ms = dataSnapshot.child("rec_msg").getValue(String.class);
                fUid = dataSnapshot.child("fromUID").getValue(String.class);
                st = dataSnapshot.child("rec_secret").getValue(String.class);

                int km = 0;
                try {
                    km = Integer.parseInt(st);
                }
                catch (NumberFormatException e) {
                    Log.d("NumberError:", "Can't convert");
                }

                if(ms!=null && !ms.equals(""))
                    addMessageBox(ms,km);
                else
                    Toast.makeText(Main5Activity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                ms = dataSnapshot.child("rec_msg").getValue(String.class);
                fUid = dataSnapshot.child("fromUID").getValue(String.class);
                st = dataSnapshot.child("rec_secret").getValue(String.class);

                int km = 0;
                try {
                    km = Integer.parseInt(st);
                }
                catch (NumberFormatException e) {
                    Log.d("NumberError:", "Can't convert");
                }


                if(ms!=null && !ms.equals(""))
                    addMessageBox(ms,km);
                else
                    Toast.makeText(Main5Activity.this,"Something went wrong",Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                if(mAuth.getUid().equals(fUid))
                    Toast.makeText(Main5Activity.this,"You left the conversation", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(Main5Activity.this,"User left the conversation", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });

奇怪的是,当我将usersRef附加到DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); final DatabaseReference usersRef = rootRef.child("users"); 时,它可以工作,但是从逻辑上讲,我只需要侦听特定于变量childEventListener的更改。

usersRef的值是需要收听的孩子的td

数据库如下所示: database

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该使用FileInfo file = new FileInfo("C:\Hello.pdf"); if ( file.isExcel()) Console.WriteLine("File is PDF"); ,它可以一次侦听单个节点。代码非常相似:

ValueEventListener