Android Firebase,获取键在节点中的位置
Android Firebase, Get position of key in node
我想根据用户的排名获取数据。首先按用户点数排序,获取用户在节点中的位置。但是我想不出实现这个的方法。
首先这是我的 firebase 'users' 节点
也许会像'databaseRef.child("users").orderByChild("points")...'
我从这里不知道。
ps。它不是第 n 个项目。这是 'where is the item'(第 n 项)
谢谢!
敬请阅读
The callback function receives a DataSnapshot, which is a snapshot of
the data. A snapshot is a picture of the data at a particular database
reference at a single point in time. Calling val() / getValue() on a
snapshot returns the a language-specific object representation of the
data. If no data exists at the reference's location, the snapshot's
value is null.
如果你想获得所有 points
然后检查这个 LOGIC.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("products");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Integer> points = new ArrayList<>();
for (Map.Entry<String, Object> entry : products.entrySet()){
Map singleUser = (Map) entry.getValue();
points.add((Integer) singleUser.get("points"));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
这可以通过在 dataSnapshot 循环中添加一个 ctr 来完成
myRef = FirebaseDatabase.getInstance().getReference().child("users");
final Query query = myRef.orderByChild("points");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int total = dataSnapshot.getChildrenCount();
int i = 0;
// loop through dataSnapshot
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String nickName = childSnapshot.child("nickName").getValue(String.class);
if (nickName.equals(mUser.getNickName()) {
//when nickName would match
int userPlace = total - i;
int points = childSnapshot.child("points").getValue(Interger.class);
//do something here
break;
} else {
i++;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我解决了这个问题。 Android_K.Doe的回答是正确的。我在他的代码中添加了一些代码。因为Firebase数据库没有给出降序查询。
final Query query = databaseReference.child("users").orderByChild("points");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 0;
int all = (int) dataSnapshot.getChildrenCount();
Log.d(TAG, "all: " + all);
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String nickName = ds.child("nickName").getValue(String.class);
if (nickName.equals(user.getNickName())) {
Log.d(TAG, "nickName: " + user.getNickName());
int userPlace = i;
Log.d(TAG, "position: " + userPlace);
myInfoRankingNumTxt.setText(Integer.toString(all - (i)) + "위");
break;
} else {
i++;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我想根据用户的排名获取数据。首先按用户点数排序,获取用户在节点中的位置。但是我想不出实现这个的方法。
首先这是我的 firebase 'users' 节点
也许会像'databaseRef.child("users").orderByChild("points")...' 我从这里不知道。
ps。它不是第 n 个项目。这是 'where is the item'(第 n 项)
谢谢!
敬请阅读
The callback function receives a DataSnapshot, which is a snapshot of the data. A snapshot is a picture of the data at a particular database reference at a single point in time. Calling val() / getValue() on a snapshot returns the a language-specific object representation of the data. If no data exists at the reference's location, the snapshot's value is null.
如果你想获得所有 points
然后检查这个 LOGIC.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("products");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Integer> points = new ArrayList<>();
for (Map.Entry<String, Object> entry : products.entrySet()){
Map singleUser = (Map) entry.getValue();
points.add((Integer) singleUser.get("points"));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
这可以通过在 dataSnapshot 循环中添加一个 ctr 来完成
myRef = FirebaseDatabase.getInstance().getReference().child("users");
final Query query = myRef.orderByChild("points");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int total = dataSnapshot.getChildrenCount();
int i = 0;
// loop through dataSnapshot
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String nickName = childSnapshot.child("nickName").getValue(String.class);
if (nickName.equals(mUser.getNickName()) {
//when nickName would match
int userPlace = total - i;
int points = childSnapshot.child("points").getValue(Interger.class);
//do something here
break;
} else {
i++;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我解决了这个问题。 Android_K.Doe的回答是正确的。我在他的代码中添加了一些代码。因为Firebase数据库没有给出降序查询。
final Query query = databaseReference.child("users").orderByChild("points");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 0;
int all = (int) dataSnapshot.getChildrenCount();
Log.d(TAG, "all: " + all);
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String nickName = ds.child("nickName").getValue(String.class);
if (nickName.equals(user.getNickName())) {
Log.d(TAG, "nickName: " + user.getNickName());
int userPlace = i;
Log.d(TAG, "position: " + userPlace);
myInfoRankingNumTxt.setText(Integer.toString(all - (i)) + "위");
break;
} else {
i++;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});