英文:
Value of snapshot returns null firebase
问题
我从Firebase获取一些图像ID,但它总是返回null,尽管我确保数据存在且键名正确。以下是我的代码和Firebase数据库:
String storeidd = getIntent().getStringExtra("storeid");
Query query = FirebaseDatabase.getInstance().getReference().child("Member").child(storeidd).child(childDataSnapshot.getKey()).child("proImages").orderByChild("ImageID").equalTo("1597332319044_0.null");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
System.out.println(snapshot.child("ImageID").getValue().toString()); // <- 返回null
itemArrayList.add(new ClassListItems(childDataSnapshot.child("proname").getValue().toString(), snapshot.child("ImageID").getValue().toString(), childDataSnapshot.child("proprice").getValue().toString(), childDataSnapshot.child("prodesc").getValue().toString()));
myAppAdapter = new MyAppAdapter(itemArrayList, showProducts.this);
listView21.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView21.setAdapter(myAppAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
另外,当我将其打印为System.out.println(snapshot.getValue().toString());
时,它给我返回{-MEcQEJFZCO5Cr0aJSbg={ImageID=1597332319044_0.null}}
。有什么想法吗?
英文:
I am getting some image ids from firebase but it always returns null even though i am 100% sure data is there and the key names are correct here is my code and firebase database:
String storeidd = getIntent().getStringExtra("storeid");
Query query = FirebaseDatabase.getInstance().getReference().child("Member").child(storeidd).child(childDataSnapshot.getKey()).child("proImages").orderByChild("ImageID").equalTo("1597332319044_0.null");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()){
System.out.println(snapshot.child("ImageID").getValue().toString()); // <- returns null
itemArrayList.add(new ClassListItems(childDataSnapshot.child("proname").getValue().toString(),snapshot.child("ImageID").getValue().toString(),childDataSnapshot.child("proprice").getValue().toString(),childDataSnapshot.child("prodesc").getValue().toString()));
myAppAdapter = new MyAppAdapter(itemArrayList, showProducts.this);
listView21.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView21.setAdapter(myAppAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Also when I print it as System.out.println(snapshot.getValue().toString());
It gives me {-MEcQEJFZCO5Cr0aJSbg={ImageID=1597332319044_0.null}}
Any ideas?
答案1
得分: 0
你需要在快照内部进行迭代以获取数据:
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()){
for(DataSnapshot ds : snapshot.getChildren()){
String imageID = ds.child("ImageID").getValue(String.class);
}
}
}
英文:
You need to iterate inside the snapshot to be able to get the data:
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()){
for(DataSnapshot ds : snapshot.getChildren()){
String imageID = ds.child("ImageID").getValue(String.class);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论