本文介绍了为什么childEventListener不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

使用 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:,无法转换);
}

if(ms!= null& ;! ms.equals())
addMessageBox(ms,km);
其他
Toast.makeText(Main5Activity.this,出了点问题,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:,无法转换);
}


if(ms!= null&!amp; ms.equals())
addMessageBox(ms,km);
else
Toast.makeText(Main5Activity.this,出了点问题,Toast.LENGTH_SHORT).show();

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot){
if(mAuth.getUid()。equals(fUid))
Toast.makeText(Main5Activity.this,您离开了对话,Toast.LENGTH_SHORT).show();
else
Toast.makeText(Main5Activity.this,用户离开了对话,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时,它可以工作code>到 usersRef ,但是从逻辑上讲,我只需要侦听特定于变量 td 的更改。 / p>

td 的值是孩子的 uid



数据库如下所示:

解决方案

在这种情况下,您应该使用 ValueEventListener ,它可以一次侦听单个节点。代码非常相似:

  DatabaseReference TUidRef = usersRef.child(td); 

TUidRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot){
ms = dataSnapshot.child( rec_msg ).getValue(String.class);
fUid = dataSnapshot.child( fromUID)。getValue(String.class);
st = dataSnapshot.child( rec_secret)。getValue(String。类);

int km = 0;
try {
km = Integer.parseInt(st);
}
catch(NumberFormatException e){
Log.d( NumberError:,无法转换);
}

if(ms!= null&&!ms.equals( ))
addMessageBox(ms,km);
其他
Toast.makeText(Main5Activity.this,出问题的地方,Toast.LENGTH_SHORT).show();
}

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


The code for using the childEventListener looks like this:

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) {

            }
        });

The usersRef is defined as:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference usersRef = rootRef.child("users");

The queer thing is that, it works when I attach the childEventListener to usersRef, but logically I need to listen to changes only specific to the variable td.

The value of td is the uid of the child needed to be listened.

The database looks like this:

解决方案

In that case you should use a ValueEventListener, which listens to a single node at once. The code is quite similar:

DatabaseReference TUidRef = usersRef.child(td);

TUidRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        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 onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

这篇关于为什么childEventListener不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:17