RecyclerView不显示来自firebase的数据

时间:2018-05-31 18:50:16

标签: java android firebase firebase-realtime-database android-recyclerview

代码

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();
        }
    });
}

保存数据

 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();

                        }
                    }
                });

数据库结构 - https://ibb.co/dJ6PdJ

如果它的名称是常量且值是变量的,我可以自己解决它我想但这里的名字是由用户的名字设置的,并且评分得分......我从未在firebase中使用过这种类型数据库如此不确定...当我运行上面的代码时,它不会显示任何空白屏幕

1 个答案:

答案 0 :(得分:0)

首先,您需要从firebase提取所有数据,并在最后一项计数时应用检查,然后将数据设置为Adapter。 请参考下面的代码以供参考。

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();
        }
    });
}