How do I get the key "-KLpcURDV68BcbAvlPFy" when I know the field "name" contains "efg" in the following structure in Firebase.
当我知道字段“name”在Firebase的以下结构中包含“efg”时,如何获得键“-KLpcURDV68BcbAvlPFy”。
clubs
-KLpcURDV68BcbAvlPFy
dept: "abc"
desc: "xyz"
name: "efg"
-asdasdasddsad
dept: "asda"
desc: "asd"
name: "adddd"
I tried this but it returned "clubs"
我试过了,但它返回了"俱乐部"
mDatabase.child("clubs").orderByChild("name").equalTo("efg").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey =dataSnapshot.getKey();
1 个解决方案
#1
13
That's because you're using a ValueEventListener
. If the query matches multiple children, it returns a list of all those children. Even if there's only a single matches child, it's still a list of one. And since you're calling getKey()
on that list, you get the key of the location where you ran the query.
这是因为您正在使用ValueEventListener。如果查询匹配多个子节点,则返回所有这些子节点的列表。即使只有一个匹配的孩子,它仍然是一个列表。由于您在该列表中调用getKey(),因此您将获得运行查询的位置的键。
To get the key of the matches children, loop over the children of the snapshot:
要获取匹配子节点的键,请对快照的子节点进行循环:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String clubkey = childSnapshot.getKey();
But note that if you assume that the club name is unique, you might as well store the clubs under their name and access the correct one without a query:
但请注意,如果您假设俱乐部名称是唯一的,您不妨将俱乐部的名称存储在它们的名称下,并访问正确的名称,而无需查询:
mDatabase.child("clubs")
.child("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey = dataSnapshot.getKey(); // will be efg
#1
13
That's because you're using a ValueEventListener
. If the query matches multiple children, it returns a list of all those children. Even if there's only a single matches child, it's still a list of one. And since you're calling getKey()
on that list, you get the key of the location where you ran the query.
这是因为您正在使用ValueEventListener。如果查询匹配多个子节点,则返回所有这些子节点的列表。即使只有一个匹配的孩子,它仍然是一个列表。由于您在该列表中调用getKey(),因此您将获得运行查询的位置的键。
To get the key of the matches children, loop over the children of the snapshot:
要获取匹配子节点的键,请对快照的子节点进行循环:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String clubkey = childSnapshot.getKey();
But note that if you assume that the club name is unique, you might as well store the clubs under their name and access the correct one without a query:
但请注意,如果您假设俱乐部名称是唯一的,您不妨将俱乐部的名称存储在它们的名称下,并访问正确的名称,而无需查询:
mDatabase.child("clubs")
.child("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey = dataSnapshot.getKey(); // will be efg