Firebase查询等待单值事件侦听器

时间:2018-07-15 12:50:15

标签: android firebase firebase-realtime-database

我正在尝试从firebase获取数据以更新适配器中的内容。我需要等待它异步加载结果,但是从不调用onDataChange。我正在使用以下代码:

class AttendanceAdapter extends PagerAdapter implements View.OnTouchListener {
    private loginInfo_Collector mUserInfoCollector;

    private FirebaseDatabase mDatabase;
    private DatabaseReference mUsersInfoRef;
    private DatabaseReference mUser_dataAttRef;




    AttendanceAdapter(Context context){
        this.context = context;
    }


    @Override
    public int getCount() {

        if(mRollDisplayList.isEmpty()) {
            loadDatabase();
            getCount();
        }



        return mRollDisplayList.size();
    }

 private void loadDatabase(){

        mDatabase = FirebaseDatabase.getInstance();

        mUsersInfoRef = mDatabase.getReference()
                .child(AttendanceActivity.mFacultyI)
                .child(AttendanceActivity.mYearI)
                .child("users");

        setViewContentsFromDb();


    }

    private void setViewContentsFromDb(){


            mUsersInfoRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (mRollList.isEmpty()) {
                        for (DataSnapshot users : dataSnapshot.getChildren()) {

                            uIdList.add(users.getKey());
                            mUserInfoCollector = users.getValue(loginInfo_Collector.class);
                            mNameList.add(mUserInfoCollector.getName());
                            mRollList.add(mUserInfoCollector.getRoll_no());
                            mPhotoUriList.add(Uri.parse(mUserInfoCollector.getPhoto_url()));

                        }


                    }
                }

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

                }
            });


    }

我尝试使用while的条件为list不为空,否则保持空循环,但list从未更新。是否有任何方法等待查询完成才返回结果?

数据库结构: 1

已解决编辑问题:在设置适配器之前将数据库代码移至,并使变量成为公共和静态变量以从适配器类访问它们。

1 个答案:

答案 0 :(得分:0)

要解决此问题,只需要以下几行代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("BCT/2072/notice_node/users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> mNameList = new ArrayList<>();
        List<String> mRollList = new ArrayList<>();
        List<String> mPhotoUriList = new ArrayList<>();

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            loginInfo_Collector mUserInfoCollector = users.getValue(loginInfo_Collector.class);

            mNameList.add(mUserInfoCollector.getName());
            mRollList.add(mUserInfoCollector.getRoll_no());
            mPhotoUriList.add(Uri.parse(mUserInfoCollector.getPhoto_url()));

            Log.d("TAG", mUserInfoCollector.getName());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);

控制台中的输出将是所有用户的名称。

请注意,一种更优雅的方法是仅创建一个List<loginInfo_Collector>并在其中添加类型loginInfo_Collector的对象,而不像您那样创建3个不同的列表。