从Firebase获取物品的位置 [英] Get the position of item from Firebase

查看:50
本文介绍了从Firebase获取物品的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了运行在排行榜列表中并返回相应值的代码.但是我需要返回该项目在列表中的位置.我应该如何修改代码,使其返回项目的位置(索引).

I have found the code that runs trough my list of leader board and returns the corresponding value. But I would need to return the position of the item in the list. How should I modify the code so it would return the position (index) of the item.

谢谢.

//query for sorting by score
    score = FirebaseDatabase.getInstance().getReference().child("Leaders");
    final Query query = score.orderByChild("uID").equalTo(mCurrentUser.getUid());

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Iterator<DataSnapshot> iterator = dataSnapshot.getChildren().iterator();

            int length = (int) dataSnapshot.getChildrenCount();

            String[] sampleString = new String[length];

            while(i < length) {
                sampleString[i] = iterator.next().getValue().toString();
                //Log.d(Integer.toString(i), sampleString[i]);

                //print the value of current score
                Toast.makeText(LeaderBoardActivity.this,sampleString[i],Toast.LENGTH_LONG).show();
                i++;
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

推荐答案

我已经根据您的评论编辑了代码.由于您只关心位置和分数...

I've edited the code based on your comments. Since you only care about the position and score...

// you will need to query the database based on scores
score = FirebaseDatabase.getInstance().getReference().child("Leaders");
final Query query = score.orderByChild("Score");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // get total leaders. Let's say total leader is 5 persons
        int total = (int) dataSnapshot.getChildrenCount();
        // let's say userB is actually 2nd in the list
        int i = 0;
        // loop through dataSnapshot
        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
            String uID = childSnapshot.child("uID").getValue();
            if (uID.equals(mCurrentUser.getUid()) {
                // the list is ascending, when finally reach userB, i = 3
                int userPlace = total - i;    // gives 2
                int score = childSnapshot.child("Score").getValue(Interger.class);
                break;
            } else {
                i++;
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

请注意,这可能会占用大量带宽.随着越来越多的用户,您将需要为每个查询下载所有用户数据.如果只显示前5名领导者,可能会更好.

Note that this can be very bandwidth consuming. As you get more and more users, you will need to download all the user data for each query. It might be better if you just show the top 5 leaders etc.

这篇关于从Firebase获取物品的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆