在比较 firebase 实时数据的两个数组列表时 returns 一个空数组列表

While comparing the two arraylists of firebase Realtime data returns an empty arraylist

正在开发一个聊天应用程序,我想获取其联系人保存在用户 (A) 手机 phone 中的用户列表(B、C、D、..)。

首先,我从 phone 中获取用户 (A) 联系人并将它们存储在 ArrayList (phoneContactArrayList) 中。其次,我获取用户在我的应用程序上注册的 phone 号码,并将它们存储在 ArrayList (dbContactArrayList).

现在我想比较这两个数组列表并从中获取常见的联系人号码,即在我的应用程序上注册的用户 (A) 的联系人 (B、C、D...)用户 (A) 可以通过我的应用与他们联系。

为此,这里是从用户 (A) 手机 phone 获取联系人的方法。

private fun getContactList() {
        phoneContactArrayList = ArrayList()
        val cr = contentResolver
        val cur = cr.query(
            ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null
        )
        if (cur?.count ?: 0 > 0) {
            while (cur != null && cur.moveToNext()) {
                val id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID))
                val name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))

                if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    val pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        arrayOf(id), null
                    )
                    while (pCur!!.moveToNext()) {
                        phoneContactArrayList?.clear()
                        val phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        phoneContactArrayList!!.add(phoneNo)
                        Log.i("Users Ph.Contacts List=" , phoneContactArrayList.toString()) 
                         // all users contacts are shown successfully as I check in Logcat
                    }
                    pCur.close()
                }
            }
        }
        cur?.close()

    }

这是通过 firebase 身份验证获取在我的应用程序上注册的用户的方法。

private fun getFirebaseContacts() {
        dbContactArrayList = ArrayList()
        FirebaseDatabase.getInstance().getReference("UserProfile")
            .addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onDataChange(contactList: DataSnapshot) {
                    try {
                        dbContactArrayList?.clear()
                        for (eachContactList in contactList.children) {
                            // Log.e("TAG", "onDataChange: " + eachContactList.value.toString())
                            var contactModel: SignUpEntity =
                                eachContactList.getValue(SignUpEntity::class.java)!!
                            val mData = contactModel.userPhone
                            if (mData != null) {
                                dbContactArrayList?.add(mData)
                            }
                            Toast.makeText(applicationContext,"Firebase Users List=${dbContactArrayList.toString()}",
                                Toast.LENGTH_SHORT
                            ).show() // successfully toast the numbers registered on app

                        }
                    } catch (e: Exception) {
                        //Log.e("Exception",e.toString())
                    }
                }

                override fun onCancelled(p0: DatabaseError) {
                    TODO("Not yet implemented")
                }
            })


    }

这里是比较两个 ArrayList 以获取公共联系人的方法,resultArrayList 始终为空。

  private fun getMatchedContacts(dbContactArrayList: ArrayList<String>?, phoneContactArrayList: ArrayList<String>?) { // here on debugging I get to know both arrayLists are of size 0.
        resultArrayList = ArrayList()
        for (s in phoneContactArrayList!!) {
            resultArrayList?.clear()
            if (dbContactArrayList!!.contains(s) && !resultArrayList!!.contains(s)) {
                resultArrayList!!.add(s)
            }
        }
        Log.e("Result Values", resultArrayList.toString())
    }

这是因为您的 resultArrayList 在 for 循环的每次迭代中都会被清除。尝试删除 resultArrayList?.clear().

private fun getMatchedContacts(dbContactArrayList: ArrayList<String>?, phoneContactArrayList: ArrayList<String>?) { // here on debugging I get to know both arrayLists are of size 0.
    resultArrayList = ArrayList()
    for (s in phoneContactArrayList!!) {
        if (dbContactArrayList!!.contains(s) && !resultArrayList!!.contains(s)) {
            resultArrayList!!.add(s)
        }
    }
    Log.e("Result Values", resultArrayList.toString())
}

如果你只想得到两个列表的共同元素,那么在Kotlin中它会很简单:

val l1 = listOf(1, 2, 3, 4, 5)
val l2 = listOf(1, 3, 5, 7, 9)
val common = l1.filter { i -> l2.contains(i) }
Log.d(TAG, common.toString())

结果将是:

[1, 3, 5]