将Firebase数据库与电话联系人列表进行比较

时间:2019-03-28 15:14:51

标签: android firebase firebase-realtime-database

我正在开发一个Android聊天应用程序。我正在尝试将电话联系人列表与“用户”子级中的firebase-database“电话”进行比较。我正在尝试通过查询获取“电话”的关键值,并与电话联系人列表进行比较,然后将其放入user模型中,该模型可以从数据库中获取聊天列表的信息。

这是我的代码

final DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("Users");
            Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {
                        String phone = "",
                                name = "";

                        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                            dataSnapshot.getKey();

                            if (childSnapshot.child("phone").getValue() != null) {
                                phone = childSnapshot.child("phone").getValue().toString();
                            }
                            if (childSnapshot.child("username").getValue() != null) {
                                name = childSnapshot.child("username").getValue().toString();
                            }
                            UserObject mUser = new UserObject(childSnapshot.getKey(), name, phone);
                            userList.add(mUser);

                                for (UserObject mContactIterator : contactList) {
                                    if (mContactIterator.getPhone().equals(mUser.getPhone())) {

                                            mUsers.clear();
                      //I think User user can not get the value 
                                            User user = childSnapshot.getValue(User.class);
                                            mUsers.add(user);
                                            final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                                            if (!user.getId().equals(firebaseUser.getUid())) {
                                                mUsers.add(user);
                                            }


                                        }
                                        userAdapter = new UserAdapter(getContext(), mUsers, false);
                                        recyclerView.setAdapter(userAdapter);


                                    }
                                }
                            }




                }

Firebase database Image

我认为在这里无法正常工作

if (!user.getId().equals(firebaseUser.getUid())) {
                                                mUsers.add(user);
                                            }

聊天列表需要显示联系人列表中的用户,但聊天列表为空结果 &我以前看过这个与StackOverflow相关的问题,但对我不起作用

1 个答案:

答案 0 :(得分:0)

我已经解决了,我的代码正在工作,谢谢

final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("Users");  
  Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {
                        String phone = "";

                        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                            dataSnapshot.getKey();

                            if (childSnapshot.child("phone").getValue() != null) {
                                phone = childSnapshot.child("phone").getValue().toString();
                            }


                            UserObject mUser = new UserObject(childSnapshot.getKey(), "", phone);
                            userList.add(mUser);



                                            mUsers.clear();
                      //I think User user can not get the value 
                                            User user = childSnapshot.getValue(User.class);
                                            mUsers.add(user);

                                            if (!user.getId().equals(firebaseUser.getUid())) {
                                                mUsers.add(user);
                                            }