0

Code

protected void onStart() {
    super.onStart();
    fetchResults();
}

private void fetchResults() {
    mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
                String user = resultSnapshot.getKey();
                String score = resultSnapshot.getValue(String.class);
                Results results = new Results(user, score);
                resultsList.add(results);
            }
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

Saving the data

 String name = RecieversName;
                HashMap<String, String> userMap = new HashMap<>();
                userMap.put("Name", name);
                userMap.put("Score", String.valueOf(mScore));
                mRef.child("Users").child(RecieversId).child("Quiz").child("Results").child(UID).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Intent intent = new Intent(TakingQuiz.this, TakingQuizDone.class);
                            intent.putExtra("RecieversId",RecieversId);
                            intent.putExtra("Score", mScore.toString());
                            startActivity(intent);
                            finish();

                        }
                    }
                });

Database structure - https://ibb.co/dJ6PdJ

If it the name was constant and the value was variable I could have solved it by myself I guess but here the name is set by the name of the user and value the score... I have never worked this type in firebase database so unsure... when I run the above code it doesn't display anything just a blank screen

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105

1 Answers1

0

Firstly, you need to fetch all data from firebase and apply the check at the last item count and set the data into Adapter. Please refer below code for your reference.

int totalItem = 0;
private void fetchResults() {
    mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
totalItem = dataSnapshot. getChildrenCount();
            for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
                String user = resultSnapshot.getKey();
                String score = resultSnapshot.getValue(String.class);
                Results results = new Results(user, score);
                resultsList.add(results);
                if(totalItem == resultsList.size())
                {
                // set the data into Adapter
                }
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}
Hkh
  • 357
  • 1
  • 10