我'我正在尝试从firebase数据库中检索数据,当您输入应用程序并且从不侦听OnDataChange时,将调用此方法。如果我将新图像上传到firebase,则调用OnDataChange并显示我引入的最后一个URL。我想检索所有数据网址,但我认为无论是实时数据库中的新数据,都会调用此方法。谢谢你的建议。
private void prepareAlbums() {
// storageReference = FirebaseStorage.getInstance().getReference().child("Images").child(FirebaseAuth.getInstance().getCurrentUser().toString());
System.out.print("user "+ firebaseAuth.getCurrentUser().getUid());
databaseReference.getRoot().child("user-images")
.child(firebaseAuth.getCurrentUser().getUid())
.addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
if (td != null) {
Iterator iterator = td.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
String url = (String) entry.getValue();
System.out.print("url "+ url);
urlArray.add(url);
}
}
}
@Override public void onCancelled(DatabaseError databaseError) {
}
});
CardImage a;
for (String s : urlArray) {
a = new CardImage("usuario1", 3, s, "madrid");
albumList.adenter code hered(a);
}
adapter.notifyDataSetChanged();
}
这是我的数据库结构
spear-e5a6a
user-images
3cAhNM75JdaeojrJ5RSjpBhonu63 // userid
-KjN5Jr31u5lPwubAwEF:
"https://firebasestorage.googleapis.com/v0/b/spe..."
-KjN5Nv0Sk8sC6dqDgrq:
"https://firebasestorage.googleapis.com/v0/b/spe..."
我解决了这个问题
databaseReference.getRoot().child("images")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
imageArray.clear();
cardList.clear();
for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange: " + imageSnapshot.getKey());
ImageInfo image = imageSnapshot.getValue(ImageInfo.class);
imageArray.add(image);
}
render(imageArray);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void render(ArrayList<ImageInfo> imgInfo) {
final ArrayList<UserInfo> userArray = new ArrayList<>();
CardImage card;
for (ImageInfo imageInfo : imgInfo) {
card = new CardImage(imageInfo.getName(), imageInfo.getRating(), imageInfo.getUrl(), imageInfo.getProvince(), imageInfo.getTimeStamp());
cardList.add(card);
}
adapter.notifyDataSetChanged();
}
images
1494101684644: //timestamp
1494101402668
comentary:
name:
province:
rating:
timeStamp:
url:
usermail:
voted:
users
//user id YMlB6t3Yp1Rs2mcpc9MstUSVD5z2:
email:
images
1494101402668: timestamp
1494101684644:
name:
我真的不知道它是否正确。