在recyclerview中获取Firebase数据

时间:2017-02-07 15:24:07

标签: android firebase firebase-realtime-database firebase-authentication

我有一个RecylerView适配器类,用于用户创建的帖子。我想要做的是通过Firebase检索帖子作者信息,然后将该信息绑定到onBindViewHolder()

中的帖子中

我的问题是,当我在getPostAuthorInfo()中调用onBindViewHolder()(用于从firebase获取数据)时,该方法是异步的,因此我无法将属性绑定为这样,因为数据(mUser)尚未准备就绪:

Picasso.with(mContext).load(mUser.getProfilePictureImageUrl()).into(viewHolder.authorsProfilePicture);

有人可以帮助我理解我是以错误的方式接近这个,还是只是没有正确地做到这一点?

谢谢。

    private void getPostAuthorInfo(final String userId) {
    mFirebaseDatabase.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            String firstName = "";
            String lastName = "";
            String dateAccountCreated = "";
            String email = "";
            String userId = "";
            String username = "";
            Boolean isMale = false;
            Boolean isFemale = false;
            Boolean isOther = false;
            String biography = "";
            String location = "";
            String profilePictureImageUrl = "";
            String profilePictureImageName = "";
            String profilePictureCaptionName = "";
            HashMap <String, String> profileInformation = new HashMap<>();
            for (DataSnapshot child : snapshot.getChildren()) {
                firstName = (String) snapshot.child("firstName").getValue();
                lastName = (String) snapshot.child("lastName").getValue();
                dateAccountCreated = (String) snapshot.child("dateAccountCreated").getValue();
                email = (String) snapshot.child("email").getValue();
                userId = (String) snapshot.child("userId").getValue();
                username = (String) snapshot.child("username").getValue();
                isMale = (Boolean) snapshot.child("male").getValue();
                isFemale = (Boolean) snapshot.child("female").getValue();
                isOther = (Boolean) snapshot.child("other").getValue();
                biography = (String) snapshot.child("biography").getValue();
                location = (String) snapshot.child("location").getValue();
                profilePictureImageUrl = (String) snapshot.child("profilePictureImageUrl").getValue();
                profilePictureImageName = (String) snapshot.child("profilePictureImageName").getValue();
                profilePictureCaptionName = (String) snapshot.child("profilePictureCaptionName").getValue();
                profileInformation = (HashMap) snapshot.child("profileInformation").getValue();
            }
            mUser = new User(userId, firstName, lastName, username, email, isMale,
                    isFemale, isOther, dateAccountCreated, biography, location,
                    profilePictureImageUrl, profilePictureImageName, profilePictureCaptionName, profileInformation);
            Log.i(TAG, "Currently signed in user: " + mUser.toString());
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.e(TAG, "Failed to retrieve signed in user", error.toException());
        }
    });
}

1 个答案:

答案 0 :(得分:1)

“帖子”是一件事你只想要作者信息吗?

您可以将回调提取到推荐的异步方法调用模式的方法中。

private void getPostAuthorInfo(final String userId, ValueEventListener listener) {
    mFirebaseDatabase.child(userId).addListenerForSingleValueEvent(listener);
}

然后,只要您调用该方法,就可以访问您的imageview。

ImageView authorsProfilePicture; 
Context mContext;

api.getPostAuthorInfo("id-xyz", new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        String profilePictureImageUrl = (String) snapshot.child("profilePictureImageUrl").getValue();

        Picasso.with(mContext)
            .load(profilePictureImageUrl)
            .into(authorsProfilePicture);
    }
});