Home » Android » Retrieved Firebase data into the top 5 android studio-Exceptionshub

Retrieved Firebase data into the top 5 android studio-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have retrieved data from the Firebase database in which i have presented it in a list view. Now i only want to display the first 5 scores and should start from the highest score to the least. I am struggling to code this in android studio.

private FirebaseAuth firebaseAuth;
private ListView listView;
DatabaseReference databaseReference;
private FirebaseDatabase firebaseDatabase;
List < ScoreProfile > scoreList;


@Override
protected void onStart() {
    super.onStart();
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot scoreSnapshot: dataSnapshot.getChildren()) {

                ScoreProfile scoreProfile = scoreSnapshot.getValue(ScoreProfile.class);
                scoreList.add(scoreProfile);

            }
            ScoreInfoAdapter scoreInfoAdapter = new ScoreInfoAdapter(RankT.this, scoreList);
            listView.setAdapter(scoreInfoAdapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank_t);
        Toolbar toolbar = findViewById(R.id.toolbarMain);
        toolbar.setTitle("GymTastic Rank Table");


        listView = findViewById(R.id.listview);

        firebaseDatabase = FirebaseDatabase.getInstance();

        databaseReference = firebaseDatabase.getReference("Bench");



        scoreList = new ArrayList < > ();

        firebaseAuth = FirebaseAuth.getInstance();

This is the code that i am using within my activity. If you require more information, please do ask.

How to&Answers: