“Value of snapshot returns null firebase” 翻译为中文为:”快照的值返回 null firebase”

huangapple go评论88阅读模式
英文:

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(&quot;storeid&quot;);
Query query = FirebaseDatabase.getInstance().getReference().child(&quot;Member&quot;).child(storeidd).child(childDataSnapshot.getKey()).child(&quot;proImages&quot;).orderByChild(&quot;ImageID&quot;).equalTo(&quot;1597332319044_0.null&quot;);
query.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
           if (snapshot.exists()){
                System.out.println(snapshot.child(&quot;ImageID&quot;).getValue().toString()); // &lt;-  returns null
                itemArrayList.add(new ClassListItems(childDataSnapshot.child(&quot;proname&quot;).getValue().toString(),snapshot.child(&quot;ImageID&quot;).getValue().toString(),childDataSnapshot.child(&quot;proprice&quot;).getValue().toString(),childDataSnapshot.child(&quot;prodesc&quot;).getValue().toString()));
                myAppAdapter = new MyAppAdapter(itemArrayList, showProducts.this);
                listView21.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                listView21.setAdapter(myAppAdapter);
            }
      }

      @Override
      public void onCancelled(@NonNull DatabaseError error) {

      }
});

“Value of snapshot returns null firebase” 翻译为中文为:”快照的值返回 null firebase”

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(&quot;ImageID&quot;).getValue(String.class);
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月14日 00:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定