RecyclerView仅在第二次启动活动时填充

时间:2019-05-12 20:45:39

标签: java android firebase firebase-realtime-database

我试图创建一个RecyclerView来从Firebase接收排序的用户密钥,然后根据排序的数据查找用户并显示它们。

我第一次启动活动时,它完全是空白的,并且没有到达给适配器提供数据的onChildAdded方法。但是,如果我退出并返回该活动,那么从那里开始一切都很好。

我尝试将代码放在onChildAdded中,放入onChildChaned中,但是这会在用户有任何更新时触发,并且列表中填充了重复项。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaderboards);
        recyclerView = findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        this.sorted = new ArrayList<>();

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        this.user = FirebaseAuth.getInstance().getCurrentUser();

        DatabaseReference stepsRef = database.getReference("Steps");
        stepsRef.orderByValue().addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userKeys.clear();
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    userKeys.add(ds.getKey());
                }
                Log.v("help", userKeys.toString());
            }

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

            }
        });

        DatabaseReference usersRef = database.getReference("Users");
        usersRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                for(int i = 0; i < userKeys.size(); i++) {
                    Log.v("number " + i, userKeys.get(i));
                        if(dataSnapshot.getKey().equals(userKeys.get(i))) {
                            Log.v("aaaaa", "aaaa");
                            User user = dataSnapshot.getValue(User.class);
                            users.add(user);
                            Log.v("username", users.get(0).getFirstName());
                    }
                }

                ArrayList<User> sorted = new ArrayList<>();

                for(int i = 0; i < userKeys.size(); i++) {
                    for(int j = 0; j < users.size(); j++) {
                        User user = users.get(j);
                        if(user.getId().equals(userKeys.get(i))) {
                            sorted.add(user);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }

                if(mAdapter == null) {
                    mAdapter = new LeaderboardsAdapter(sorted);
                    Log.v("plshelp", sorted.toString());
                    recyclerView.setAdapter(mAdapter);
                } else {
                    ((LeaderboardsAdapter)mAdapter).updateData(sorted);
                }

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });
        mAdapter = new LeaderboardsAdapter(sorted);
        recyclerView.setAdapter(mAdapter);
    }

有没有办法让适配器在第一次启动时加载数据?

0 个答案:

没有答案