Firebase记分板显示多余的用户名和分数

时间:2018-12-26 15:06:17

标签: java firebase firebase-realtime-database hashmap

我正在尝试为我的应用编写记分板。我已经完成了计分板的大部分工作,但是有一个我无法解决的问题。当我在Firebase数据库中更改分数时,排名会显示多余的名称和分数

我的数据库结构:

enter image description here

更改前的记分板:

enter image description here

更改后的记分板:

enter image description here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);

        String username = getIntent().getStringExtra("UserName");
        currentuser = username;
        un1 = (TextView)findViewById(R.id.textusernameplatz1);
        un2 = (TextView)findViewById(R.id.textusernameplatz2);
        un3 = (TextView)findViewById(R.id.textusernameplatz3);

        s1 = (TextView)findViewById(R.id.textscoreplatz1);
        s2 = (TextView)findViewById(R.id.textscoreplatz2);
        s3 = (TextView)findViewById(R.id.textscoreplatz3);

        databaseReference = FirebaseDatabase.getInstance().getReference("Users");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                // username & score von Datenbank lesen
                for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                    String userkey = dsp.getKey();
                    String scores = dsp.child("scorehigh").getValue().toString();
                    String userName = dsp.child("username").getValue().toString();
                    Integer scoresint = Integer.parseInt(scores);

                    RankingData rd = new RankingData(scoresint, userName);

                    rankingData.add(rd);
                    Collections.sort(rankingData);

                    }

                    // probleme bei refresh: Lösung evtl: exist() methode coden, damit
                // ein Username nicht doppel auftreten kann
                un1.setText(rankingData.get(0).getUsername());
                un2.setText(rankingData.get(1).getUsername());
                un3.setText(rankingData.get(2).getUsername());

                s1.setText(rankingData.get(0).getScore().toString());
                s2.setText(rankingData.get(1).getScore().toString());
                s3.setText(rankingData.get(2).getScore().toString());

            }
            @Override
            public void onCancelled(DatabaseError error) {

            }
        });

我的其他班级:

public RankingData(Integer score, String username) {
    this.score = score;
    this.username = username;
}

public Integer getScore() {
    return score;
}

public void setScore(Integer score) {
    this.score = score;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Override
public int compareTo(RankingData b){
    if(this.getScore() < b.getScore()){
        return 1;
    }
    if(this.getScore() > b.getScore()){
        return -1;
    }
return 0;
}

0 个答案:

没有答案