如何处理Firebase数据库的异步性质

时间:2019-07-03 08:43:06

标签: java android firebase firebase-realtime-database

由于我的测验应用中Firebase数据库的异步特性,我遇到了这个问题。问题分数包含当前用户及其分数。排名包含用户及其相应的分数。问题分数已将分数存储在其中,但是当我我正在用分数更新排名数据库...排名对象中的标记值被重置为零。我应该如何处理?

我已经尝试使用排名回调来更新数据库,但是没有运气。

public class RankingFragment extends Fragment {
    View myFragment;
    RecyclerView rankingList;
    LinearLayoutManager layoutManager;
    FirebaseRecyclerAdapter<Ranking, RankingViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference questionScore, rankingTbl;
    String k;
    int sum=0;

    public static RankingFragment newInstance() {
        RankingFragment rankingFragment = new RankingFragment();
        return rankingFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        database = FirebaseDatabase.getInstance();
        questionScore = database.getReference("Question_Score");
        rankingTbl = database.getReference("Ranking");
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        myFragment = inflater.inflate(R.layout.fragment_ranking, container, false);
        rankingList = (RecyclerView) myFragment.findViewById(R.id.rankingList);
        layoutManager = new LinearLayoutManager(getActivity());
        rankingList.setHasFixedSize(true);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        rankingList.setLayoutManager(layoutManager);


        updateScore(Common.currentUser.getUsername());
        return myFragment;

    }

    private void updateScore(final String username)
    {
        questionScore.orderByChild("user").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    QuestionScore ques = data.getValue(QuestionScore.class);
                    sum+=Integer.parseInt(ques.getScore());
                }
                Ranking ranking = new Ranking(username, sum);
                rankingTbl.child(ranking.getUsername()).setValue(ranking);
                adapter = new FirebaseRecyclerAdapter<Ranking, RankingViewHolder>(
                        Ranking.class, R.layout.layout_ranking, RankingViewHolder.class, rankingTbl.orderByChild("marks")
                ) {
                    @Override
                    protected void populateViewHolder(final RankingViewHolder viewHolder, Ranking model, int position) {
                        viewHolder.txt_name.setText(model.getUsername());
                        viewHolder.txt_score.setText(String.valueOf(model.getMarks()));

                    }
                };
                finish();


            }

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

            }
        });

    }

    private void finish() {
        adapter.notifyDataSetChanged();
        rankingList.setAdapter(adapter);
    }
}

排名数据库的标记存储0,因此视图持有者也填充零,而视图存储者应该存储当前用户的分数(存储在“ sum + = Integer.parseInt(ques.getScore())中”)。

1 个答案:

答案 0 :(得分:0)

您正在将QuestionScore的值分配给sum,就像添加所有QuestionScore值一样

for (DataSnapshot data : dataSnapshot.getChildren()) {
    QuestionScore ques = data.getValue(QuestionScore.class);
    sum+=Integer.parseInt(ques.getScore()); // <--- changed = to +=
}