Firebase Realtime数据库getvalue()未映射对象 [英] Firebase Realtime database getvalue() not mapping the object
问题描述
这是我的Firebase聊天结构(请参见下图).我只想获取amitpal_name值,而不是其他值,但是此方法将我的所有值都提取了
我也曾经使用过
This is my firebase chat structure(see image below). I want to get only amitpal_name value not the others but this method fetching me all the values
I had also use this
dbreference= FirebaseDatabase.getInstance().getReference( "ChatMessage" ).child( "msg");
dbreference.orderByChild("Ref").addChildEventListener( new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {[![enter image description here][1]][1]
Iterator iterator=dataSnapshot.getChildren().iterator();
while (iterator.hasNext()){
String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
Log.e( "ADDED IS-" , chatis);
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Iterator iterator=dataSnapshot.getChildren().iterator();
while (iterator.hasNext()){
String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
Log.e( "ADDED chng-" , chatis);
}
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e( "ERRO FBDB-", databaseError.getMessage());
}
} );
dbreference = FirebaseDatabase.getInstance().getReference("ChatMessage").child("msg").child("amitpal_name");
但是它没有提供任何信息或值,请在这里帮助我
but it not providing any info or values pls help me here
我也尝试使用以下方法给我良好的预期结果,但是主要问题是,当我在不使用服务的情况下在firebaserealtime Chat中更新数据库时,我想在listview中反映新值,因为我需要以下方法
I have also try this below method giving me good and expected result but the main problem is i want to reflect the new value in listview when databse update in firebaserealtime Chat without using service, for that i need this method below
public void onChildChanged(@NonNull DataSnapshot dataSnapshot,@Nullable String s)
推荐答案
这是您的参考
refAmitPat = FirebaseDatabase.getInstance().getReference().child("ChatMessage").child("msg");
这是您的数据库参考上的子事件列表器
and here is child event listner on your DB Reference
refAmitPat.child("amitpal_name").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d("amitPal",dataSnapshot.getValue()+"");
// add message value to list and call notify datasetchange method
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d("amitPal",dataSnapshot.getValue()+"");
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
您在做什么,错误的是,您正在迭代数据快照,而当您将消息以用户名的身份推送到用户名下时,孩子将以childAdded方法中的datasnapshot的身份获得,这意味着您正在迭代的数据快照实际上就是您的消息带有按键和消息值.因此无需进行遍历.只需调用datasnapshot.getValue()函数即可获取消息值
what are you doing, wrong is, you are iterating the datasnapshot whereas when you push the message under usernames as child, the child will get as the datasnapshot in childAdded method, means the data snapshot your are iterating, is actually your message with push-key and message value. so there's no need to iterate through that. simply call datasnapshot.getValue() function to get message value
这篇关于Firebase Realtime数据库getvalue()未映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!