Always use Contact.getNameToDisplay()

Because Contact.name is empty on some android versions
This commit is contained in:
Naveen 2023-05-05 22:38:50 +05:30
parent 01c771da91
commit ea5adfcf91
2 changed files with 11 additions and 8 deletions

View File

@ -1536,7 +1536,7 @@ class EditContactActivity : ContactActivity() {
} }
private fun setupAutofill(nameTextViews: List<MyAutoCompleteTextView>) { private fun setupAutofill(nameTextViews: List<MyAutoCompleteTextView>) {
ContactsHelper(this).getContacts { contacts -> ContactsHelper(this).getContacts(getAll = true) { contacts ->
val adapter = AutoCompleteTextViewAdapter(this, contacts) val adapter = AutoCompleteTextViewAdapter(this, contacts)
val handler = Handler(mainLooper) val handler = Handler(mainLooper)
nameTextViews.forEach { view -> nameTextViews.forEach { view ->

View File

@ -21,26 +21,29 @@ import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_au
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_name import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_name
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_number import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_number
class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: ArrayList<Contact>, var enableAutoFill: Boolean = false) : ArrayAdapter<Contact>(activity, 0, contacts) { class AutoCompleteTextViewAdapter(
val activity: SimpleActivity,
val contacts: ArrayList<Contact>,
var enableAutoFill: Boolean = false
) : ArrayAdapter<Contact>(activity, 0, contacts) {
var resultList = ArrayList<Contact>() var resultList = ArrayList<Contact>()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val contact = resultList[position] val contact = resultList[position]
var listItem = convertView var listItem = convertView
if (listItem == null || listItem.tag != contact.name.isNotEmpty()) { val nameToUse = contact.getNameToDisplay()
if (listItem == null || listItem.tag != nameToUse.isNotEmpty()) {
listItem = LayoutInflater.from(activity).inflate(R.layout.item_autocomplete_name_number, parent, false) listItem = LayoutInflater.from(activity).inflate(R.layout.item_autocomplete_name_number, parent, false)
} }
val nameToUse = contact.name
val placeholder = BitmapDrawable(activity.resources, SimpleContactsHelper(context).getContactLetterIcon(nameToUse)) val placeholder = BitmapDrawable(activity.resources, SimpleContactsHelper(context).getContactLetterIcon(nameToUse))
listItem!!.apply { listItem!!.apply {
setBackgroundColor(context.getProperBackgroundColor()) setBackgroundColor(context.getProperBackgroundColor())
item_autocomplete_name.setTextColor(context.getProperTextColor()) item_autocomplete_name.setTextColor(context.getProperTextColor())
item_autocomplete_number.setTextColor(context.getProperTextColor()) item_autocomplete_number.setTextColor(context.getProperTextColor())
tag = contact.name.isNotEmpty() tag = nameToUse.isNotEmpty()
item_autocomplete_name.text = contact.name item_autocomplete_name.text = nameToUse
item_autocomplete_number.text = contact.phoneNumbers.run { item_autocomplete_number.text = contact.phoneNumbers.run {
firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber
} }
@ -70,7 +73,7 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar
if (enableAutoFill) { if (enableAutoFill) {
val searchString = constraint.toString().normalizeString() val searchString = constraint.toString().normalizeString()
contacts.forEach { contacts.forEach {
if (it.name.contains(searchString, true)) { if (it.getNameToDisplay().contains(searchString, true)) {
resultList.add(it) resultList.add(it)
} }
} }