在聊天應用程式上作業,我想獲取其聯系人保存在用戶(A)手機中的用戶串列(B、C、D、..)。
首先,我從手機中獲取用戶(A)聯系人并將它們存盤在 ArrayList (phoneContactArrayList)中。其次,我獲取在我的應用程式上注冊的用戶電話號碼,并將它們存盤在 ArrayList (dbContactArrayList)中。
現在我想比較這兩個陣列串列并從中獲取常見的聯系人號碼,即在我的應用程式上注冊的用戶(A)和用戶(A)的聯系人(B,C,D,...) ) 可以通過我的應用程式聯系他們。
為此,這里是從 User(A) 手機中獲取聯系人的方法。
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())
}
uj5u.com熱心網友回復:
這是因為您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())
}
uj5u.com熱心網友回復:
如果你只想獲取兩個串列的共同元素,那么在 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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411100.html
標籤:
