如何从消防基地获取电话号码

问题描述 投票:0回答:1

我试着将我的电话簿和已经有账户的联系人进行比较。

我设法将电话簿显示在Recycler View中,但当我尝试与Firebase进行比较时,不显示任何东西。

主要目的是只显示使用该应用的联系人。

调试:DataSnapshot { key = Users, value = null }。

enter image description here

    private void getContactList() {

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    assert phones != null;
    while (phones.moveToNext()) {

        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Contacts mContacts = new Contacts(name, phone);
        contactsList.add(mContacts);
        Collections.sort(contactsList, new Contacts(name, phone));

        getUserInfo(mContacts);
    }
}

private void getUserInfo(Contacts mContact) {

    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 = "";
                String name = "";

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

                    if (childSnapshot.child("phone").getValue() != null)

                        phone = dataSnapshot.child("phone").getValue().toString();
                    Log.i(TAG, "phone ---------+++++++++++//////////////////" + phone);

                    if (childSnapshot.child("name").getValue() != null)

                        name = childSnapshot.child("name").getValue().toString();

                    Contacts mUser = new Contacts(name, phone);
                    userList.add(mUser);
                    mUserListAdapter.notifyDataSetChanged();
                    Collections.sort(userList, new Contacts(name, phone));
                    return;


                }
            }
        }

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

            String err = databaseError.toString();

            Toast.makeText(mContext, "error" + err, Toast.LENGTH_SHORT).show();
            Log.i(TAG, "err------------+++++++++++++++++++----------------" + err);
        }
    });
}               
android firebase firebase-realtime-database
1个回答
0
投票

你缺少的是 UserPhone 在你的代码中,你的JSON的级别。

所以,类似这样。

if (childSnapshot.child("UserPhone/phone").getValue() != null)

    phone = dataSnapshot.child("UserPhone/phone").getValue().toString();
    ...

或者更习惯一点:

DataSnapshot phoneSnapshot = childSnapshot.child("UserPhone/phone");
if (phoneSnapshot.exists())
    phone = phoneSnapshot.getValue(String.class);
    ...
© www.soinside.com 2019 - 2024. All rights reserved.