检索当前用户的DataSnapshot Firebase

时间:2017-01-08 20:38:49

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

更新:我已经决定重新提出问题,以便更好地澄清问题并使用更专注的代码。

我遇到的问题是我有一个UserList对象的ArrayList,它是上一个活动中的选定用户,我们称之为selectedUsers。我有另一个对象,我想将selectedUsers以及Firebase中的当前用户分组。这将被称为mGroupMembers,它包含GroupMember对象。

我能够将selectedUsers放入mGroupMembers的ArrayList中,但我无法将当前用户(由其ID引用)放入所述ArrayList中。我认为问题在于Firebase。我不确定为什么当我拨打addListenerForSingleValueEvent时,当前用户的数据没有添加到mGroupMembers ArrayList?

这是我的代码:

public class TestActivity extends AppCompatActivity {

    //Tag
    private static String TAG = "TestActivity";

    private List<UserList> selectedUsers;
    private TextView mPickedText;
    private TextView mCurrentText;
    private TextView mMembCountText;
    private List<GroupMembers> mGroupMembers;
    private String memberNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        //Selected users from previous activity.
        selectedUsers = (List<UserList>) getIntent().getSerializableExtra("users");

        //Text Views
        mPickedText = (TextView) findViewById(R.id.first_text);
        mCurrentText = (TextView) findViewById(R.id.second_text);
        mMembCountText = (TextView) findViewById(R.id.third_text);

        //GroupMembs
        mGroupMembers = new ArrayList<>();

        //Current user.
        final String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users").child(currentUserID);
        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                UserList user = dataSnapshot.getValue(UserList.class);
                GroupMembers member;

                //If the current user...
                if (Objects.equals(user.getUid(), currentUserID)) {
                    mCurrentText.setText("Current: " + user.getName());
                    member = new GroupMembers(user.getName(), user.getUsername(), user.getEmail(), user.getUid());

                    Log.d(TAG, "Member object contains...Name: " + user.getName() + "\n" +
                            "Username: " + user.getUsername() + "\n" +
                            "Email: " + user.getEmail() +
                            "uid: " + user.getUid());

                    selectedUsers.add(user);

                    mGroupMembers.add(member);

                    for (GroupMembers members : mGroupMembers) {
                        Log.d(TAG, "Member object contains...Name: " + members.getName() + "\n" +
                                "Username: " + members.getUsername() + "\n" +
                                "Email: " + members.getEmail() +
                                "uid: " + members.getUid());
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //Picked user.
        for (UserList selected : selectedUsers) {
            GroupMembers member = new GroupMembers();

            member.setName(selected.getName());
            member.setUsername(selected.getUsername());
            member.setEmail(selected.getEmail());
            member.setUid(selected.getUid());

            mGroupMembers.add(member);

            mPickedText.setText("Selected: " + selected.getName());
        }

        for (GroupMembers members : mGroupMembers) {
            memberNames = members.getName();
        }



        mMembCountText.setText("Group Members: " + mGroupMembers.size() + " ");
    }
}

这是Firebase数据库

{
  "users" : {
    "S3l3yRh8TqRYNeTWQ7tKEdUe2K03" : {
      "email" : "antwon@domain.com",
      "name" : "Antwon ",
      "profilePicture" : " ",
      "uid" : "S3l3yRh8TqRYNeTWQ7tKEdUe2K03",
      "username" : "ant01"
    },
    "af7KgxP7QgZT3d6adtgwBRwnsaI2" : {
      "email" : "bill@domain.com",
      "name" : "Bill",
      "profilePicture" : " ",
      "uid" : "af7KgxP7QgZT3d6adtgwBRwnsaI2",
      "username" : "Bill123"
    }
  }
}

这是我记录mGroupMembers ArrayList:

时会发生什么
D/TestActivity: Member object contains...Name: Antwon 
                Username: ant01
                Email: antwon@domain.comuid: S3l3yRh8TqRYNeTWQ7tKEdUe2K03
D/TestActivity: Member object contains...Name: Bill
                Username: Bill123
                Email: bill@domain.comuid: af7KgxP7QgZT3d6adtgwBRwnsaI2
D/TestActivity: Member object contains...Name: Antwon 
                Username: ant01
                Email: antwon@domain.comuid: S3l3yRh8TqRYNeTWQ7tKEdUe2K03

正在添加当前用户(Antwon),但为什么在运行程序时不显示?现在,程序只显示所选用户的姓名以及当前用户的姓名。然后它显示mGroupMembers的大小。当然,当它应该是两个时,它的大小只有1个。

1 个答案:

答案 0 :(得分:1)

意外行为是Firebase侦听器异步性质的结果。在从Firebase服务器检索数据快照之前,getUserInfo不会执行。这通常需要几毫秒,并且将在添加侦听器之后的语句之后发生。对于您的代码,在onDataChange()触发并添加当前用户之前执行此for语句:

onDataChange()

this answer中提供了对Firebase异步数据加载的进一步说明。