问题描述
我在我的android应用程序中使用了firebase,我的树看起来像这样
data>坦克> username1>杀死= 1,尝试= 3
> username5>杀死= 5,尝试= 13
> username7>杀= 3,试= 7
> username2>杀死= 6,尝试= 4
数据>大炮> username9>杀死= 31,尝试= 33
> username7>杀死= 15,尝试= 13
> username3>杀死= 13,尝试= 77
> username6> kills = 66,tries = 44
我要做的是向用户显示前10个用户名杀死大炮或坦克(取决于哪一个用户选择),但我似乎无法找到一个解决方案。
起初我认为解决方案将是像
ref.child(data / tanks)。orderbyChild(kill)
但似乎不起作用,问题是它不会返回杀死子元素。它返回我已经可以访问的坦克的整个树。我想要的是如果可能的话,得到所有杀死孩子的名单?我不知道我怎么能得到最高的杀伤力,因为没有办法知道哪些用户名在坦克或大炮,除非我遍历所有的用户名。
>任何人都可以帮我解决这个问题吗?
DatabaseReference myref = FirebaseDatabase.getInstance ).getReference()chield( 罐)。
myref.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
$ b $ userlist = new ArrayList< String> ();
Toast.makeText(DataFromFirebase.this,no of usersare+ dataSnapshot.getChildrenCount(),Toast.LENGTH_LONG).show();
// String value =( (DataSnapshot dsp:dataSnapshot.getChildren()){
userlist.add(String.valueOf(String))dataSnapshot.getValue();
//结果将被保留在这里
(dsp.getKey())); /
}
@Override
public void onCancelled(DatabaseError firebaseError){
使用上面的代码,您可以获得tank中的所有用户,然后对所有用户引用重复此操作, / p>
I am using firebase in my android application and my tree looks like this
data > tanks > username1 > kills = 1, tries = 3
> username5 > kills = 5, tries = 13
> username7 > kills = 3, tries = 7
> username2 > kills = 6, tries = 4
data > cannons > username9 > kills = 31, tries = 33
> username7 > kills = 15, tries = 13
> username3 > kills = 13, tries = 77
> username6 > kills = 66, tries = 44
what i am trying to do is show the user the top 10 usernames with the highest kills for cannons or tanks (depending on which one the user selects) but i cant seem to find a solution.
At first i thought the solution would be like
ref.child("data/tanks").orderbyChild("kills")
but it doesn't seem to work the problem is that it doesn't return the "kills" child. it returns the whole tree of the tank which i can already access. What i want is to get a list of all "kills" child if that's possible? And i am not sure how else can i get the highest kills because there's no way of knowing which of the usernames are in tanks or cannons unless i iterate through all of the usernames.
Can anyone help me with this?
DatabaseReference myref= FirebaseDatabase.getInstance().getReference().chield("tanks");
myref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userlist= new ArrayList<String>();
Toast.makeText(DataFromFirebase.this, "no of usersare " + dataSnapshot.getChildrenCount(), Toast.LENGTH_LONG).show();
//String value = (String) dataSnapshot.getValue();
// Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
userlist.add(String.valueOf(dsp.getKey())); /
}
@Override
public void onCancelled(DatabaseError firebaseError) {
}
}
using above code you get the all users in tanks chield then repeat this for all user references then get all kill values
这篇关于查询获得前10名最高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!