Java 使用ListView制作排行榜

Java 使用ListView制作排行榜,java,android,Java,Android,我试图用listView在排行榜上显示我的用户的分数。 由于某些原因,我只能得到第一个用户的电子邮件和分数,我不知道如何让其余的用户出现在屏幕上 可能它们没有被添加到listview中,或者循环是错误的 package com.example.securityapp; 导入androidx.appcompat.app.appcompat活动; 导入android.nfc.Tag; 导入android.os.Bundle; 导入android.util.Log; 导入android.widget

我试图用listView在排行榜上显示我的用户的分数。 由于某些原因,我只能得到第一个用户的电子邮件和分数,我不知道如何让其余的用户出现在屏幕上

可能它们没有被添加到listview中,或者循环是错误的

package com.example.securityapp;
导入androidx.appcompat.app.appcompat活动;
导入android.nfc.Tag;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.database.DataSnapshot;
导入com.google.firebase.database.DatabaseError;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.database.Query;
导入com.google.firebase.database.ValueEventListener;
导入java.util.ArrayList;
导入java.util.List;
导入静态java.lang.System.in;
公共类排行榜扩展了AppCompative活动{
数据库参考数据库用户;
FirebaseDatabase=FirebaseDatabase.getInstance();
FirebaseAuth mAuth;
私有静态最终字符串TAG=“排行榜”;
字符串s;
列表视图列表视图;
数组列表;
ArrayAdapter ArrayAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_排行榜);
mAuth=FirebaseAuth.getInstance();
FirebaseUser=mAuth.getCurrentUser();
listView=(listView)findViewById(R.id.list);
列表=新的ArrayList();
arrayAdapter=新的arrayAdapter(getApplicationContext(),android.R.layout.,list);
排行榜();
}
公开作废排行榜(){
database.getReference().child(“分数”).addValueEventListener(新的ValueEventListener()){
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot快照:DataSnapshot.getChildren()){
System.out.println(“分数是:”+snapshot.child(“分数”).getValue());
s=s+snapshot.child(“用户名”).getValue()+“”+snapshot.child(“点”).getValue()+“\n”;
列表。添加(s);
setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
//获取Post失败,请记录消息
w(标记“loadPost:onCancelled”,databaseError.toException());
}
});
}

}
您正在for循环中设置适配器

替换

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    System.out.println("The score is: " + snapshot.child("Points").getValue());
                    s = snapshot.child("Username").getValue() + "         " + snapshot.child("Points").getValue() + "\n";
                    list.add(s);
                    listView.setAdapter(arrayAdapter);
                    arrayAdapter.notifyDataSetChanged();

                }

if(dataSnapshot.exist()){
列表=新的ArrayList();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
System.out.println(“分数是:”+snapshot.child(“分数”).getValue());
字符串s=snapshot.child(“用户名”).getValue()+“”+snapshot.child(“点”).getValue()+“\n”;
列表。添加(s);
}
arrayAdapter=新的arrayAdapter(getApplicationContext(),android.R.layout.,list);
setAdapter(arrayAdapter);
}

当您调用addvalue事件侦听器(侦听实时更改)时,您必须在数据发生更改时初始化列表并在循环中声明“s”。

这样可以显示所有用户,但循环的每次迭代都会显示,即第一个列表项为user1。下一个列表项是User1,User2。接下来是User1、User2、User3等等。有没有办法只获取列表的最后一次迭代而不显示全部?是的,您可以使用database.getReference().child(“Scores”).orderByKey().limitToLast(1).ad。。。别忘了标记Accepted嘿,这似乎只显示列表的最后一个值。我的意思是每次添加一个项目时,整个列表都会打印出来。我已经编辑了问题以显示我看到的内容:)忽略空值,有一个用户没有电子邮件。我已经更新了答案,只需删除arrayAdapter.notifyDataSetChanged();不幸的是,当我删除它时,我仍然遇到同样的问题
if(dataSnapshot.exist()){
     list = new ArrayList<String>();
     for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        System.out.println("The score is: " + snapshot.child("Points").getValue());
                       String s =  snapshot.child("Username").getValue() + "         " + snapshot.child("Points").getValue() + "\n";
                        list.add(s); 
                    }
    arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout., list);
    listView.setAdapter(arrayAdapter);

    }