Im trying to display the scores of my users on a leaderboard using a listView. For some reason I can only the get the email and score of the first user to appear and Im not sure how to get the remaining users to appear on screen.
Perhaps they are not being added to the listview or maybe the loop is wrong?
package com.example.securityapp;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import static java.lang.System.in;
public class leaderboard extends AppCompatActivity {
DatabaseReference databaseUsers;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth mAuth;
private static final String TAG = "leaderboard";
String s;
ListView listView;
ArrayList list;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
listView = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout., list);
leaderBoard();
}
public void leaderBoard(){
database.getReference().child("Scores").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
System.out.println("The score is: " + snapshot.child("Points").getValue());
s = s + snapshot.child("Username").getValue() + " " + snapshot.child("Points").getValue() + "\n";
list.add(s);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".leaderboard">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">
<TextView
android:layout_width="420dp"
android:layout_height="150dp"
android:gravity="center"
android:text="Leaderboard!"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#0C40F1"
android:textSize="50dp"
android:textStyle="bold">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:weightSum="2">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Points"
android:layout_gravity="left"
android:textSize="20dp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="User"
android:textSize="20dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</RelativeLayout>
</LinearLayout>
</ScrollView>