Java 将firebase实时数据库中的数据读入文本视图
我想从firebase实时数据库中读取数据,并在文本视图中设置检索到的数据,以下是我迄今为止所做的尝试Java 将firebase实时数据库中的数据读入文本视图,java,android,firebase-realtime-database,Java,Android,Firebase Realtime Database,我想从firebase实时数据库中读取数据,并在文本视图中设置检索到的数据,以下是我迄今为止所做的尝试 private DatabaseReference buckybarnz; ......... buckybarnz = FirebaseDatabase.getInstance().getReference().child(userID).child("chel_sea"); ValueEventListener eventListener = buckybarnz.add
private DatabaseReference buckybarnz;
.........
buckybarnz = FirebaseDatabase.getInstance().getReference().child(userID).child("chel_sea");
ValueEventListener eventListener = buckybarnz.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
if (dataSnapshot1 != null) {
DRive dRive = dataSnapshot1.getValue(DRive.class);
if (dRive != null) {
String receipt = dRive.getExt_ref().trim();
String text = dRive.getTxt_Ref().trim();
chalwe.setText(text);
johns.setText(receipt);
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
什么都不起作用,即使数据库中有数据,TextView也不会发生任何变化
请帮助,解决此问题的最佳方法是什么,即使是使用string类方法的解决方案也是有益的。以下是我的数据库视图]与您相比,我访问数据库的方式有所不同:
Firebase.setAndroidContext(this);
Firebase reference1 = new Firebase("https://project-id.firebaseio.com/messages/"); //replace project-id by your project-id
reference1.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println("reference4: data snapshot key"+dataSnapshot.getKey()+"");
//your code
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
希望这有帮助与您相比,我访问数据库的方式有所不同:
Firebase.setAndroidContext(this);
Firebase reference1 = new Firebase("https://project-id.firebaseio.com/messages/"); //replace project-id by your project-id
reference1.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println("reference4: data snapshot key"+dataSnapshot.getKey()+"");
//your code
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
希望这有帮助首先,您不必编写for循环,这应该是您在我的代码的onchildaded()方法中编辑的代码:
if (dataSnapshot != null) {
DRive dRive = dataSnapshot.getValue(DRive.class);
if (dRive != null) {
String receipt = dRive.getExt_ref().trim();
String text = dRive.getTxt_Ref().trim();
chalwe.setText(text);
johns.setText(receipt);
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
}
希望这能起作用首先,您不必编写for循环,这应该是您在我的代码的onchildaded()方法中编辑的代码:
if (dataSnapshot != null) {
DRive dRive = dataSnapshot.getValue(DRive.class);
if (dRive != null) {
String receipt = dRive.getExt_ref().trim();
String text = dRive.getTxt_Ref().trim();
chalwe.setText(text);
johns.setText(receipt);
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
} else {
String a = "no pending refunds available";
anthony.setText(a);
chalwe.setText(a);
johns.setText(a);
}
}
希望这能起作用而不是使用您的表名。如果您遇到任何问题,请务必告诉我。例如,您将如何从数据库中检索“ext\u ref”和txt\u ref。因为当我把我的代码放在上面所说的“你的代码”下面时,我收到了一个错误“试图调用虚拟方法java.lang.String.trim on null对象引用at on child added”这个错误在行“String text=dRive.getExt_ref().trim();”我在第二个答案中进一步解释了它,请告诉我它是否适用于您。现在错误是无法将java.lang.string类型的对象转换为com.chomba.Drive类型,此错误与codecan相同。请在问题中添加驱动器类,而不是/messages使用您的表名。如果您遇到任何问题,请务必告诉我。例如,您将如何从数据库中检索“ext\u ref”和txt\u ref。因为当我把我的代码放在上面所说的“你的代码”下面时,我收到了一个错误“试图调用虚拟方法java.lang.String.trim on null对象引用at on child added”这个错误在行“String text=dRive.getExt_ref().trim();”我在第二个答案中进一步解释过,请告诉我它是否适用于您。现在错误是无法将java.lang.string类型的对象转换为com.chomba.Drive类型,此错误与codecan相同。请在问题中添加驱动器类。请提供更详细的架构,其中应包括上层节点。请提供更详细的架构,其中应包括上层节点。