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

Time:01-15

Working on a chatting app I want to get the list of users(B, C, D,..) whose contacts are saved in the user (A) mobile phone.

First I fetch user(A) contacts from the phone and store them in an ArrayList (phoneContactArrayList). Secondly, I fetch the user's phone numbers registered on my app and also store them in ArrayList (dbContactArrayList).

Now I want to compare both of these array lists and get the common contacts numbers out of them, which are those contacts(B, C, D,...) of the user(A) registered on my app and the user(A) can contact them via my app.

For this here is the method to fetch contacts from the User(A) mobile phones.

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()

    }

Here is the method for fetching the users registered on my app via firebase authentication.

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")
                }
            })


    }

And here is the method in which both ArrayList are compared to get the common contacts, the resultArrayList is always empty.

  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())
    }

CodePudding user response:

It's because your resultArrayList is being cleared at each iteration of your for loop. Try to remove 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())
}

CodePudding user response:

If you only want to get the common elements of two lists, then in Kotlin it will be as simple as:

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())

The result will be:

[1, 3, 5]
  •  Tags:  
  • Related