在检索数据时我正在使用此代码
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Firebasemodel firebasemodel = data.getValue(Firebasemodel.class);
firebasemodels.add(firebasemodel);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});`
此代码片段返回firebase数据库的完整数据。
但我想检索特定用户的完整数据。我如何实现它?
提前致谢
答案 0 :(得分:0)
尝试以下代码:
FirebaseDatabase.getInstance().getReference().child("notepad-secure/notepad").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
while (dataSnapshots.hasNext()) {
user = dataSnapshotChild.getValue(User.class);
users_list.add(user);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Error", "---" + databaseError.getMessage());
}
});
模特课:
public class User {
public String id;
public String note;
public String tittle;
public User() {
}
public User(String id, String note, String tittle) {
this.id= id;
this.note= note;
this.tittle= tittle;
}
}
并声明
List<User> users_list;
User user;
答案 1 :(得分:0)
请使用此代码获取所有数据:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference notepadRef = rootRef.child("notepad-secure").child("notepad");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String id = ds.child("id").getValue(String.class);
String note = ds.child("note").getValue(String.class);
String title = ds.child("title").getValue(String.class);
String user = ds.child("user").getValue(String.class);
Log.d("TAG", id + " / " + note + " / " + title + " / " + users);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
notepadRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
-Kono2LMSe... / sadsad / sadsad / NK46X......
如果您只想检索特定数据,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference notepadRef = rootRef.child("notepad-secure").child("notepad").child("NK46...");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String id = ds.child("id").getValue(String.class);
String note = ds.child("note").getValue(String.class);
String title = ds.child("title").getValue(String.class);
String user = ds.child("user").getValue(String.class);
Log.d("TAG", id + " / " + note + " / " + title + " / " + users);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
notepadRef.addListenerForSingleValueEvent(eventListener);
它肯定会起作用。