Merge pull request #729 from naveensingh/fix_adapter_crash
Avoid modifying items while list is being updated
This commit is contained in:
commit
166998f86e
|
@ -51,24 +51,27 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar
|
||||||
override fun performFiltering(constraint: CharSequence?): FilterResults {
|
override fun performFiltering(constraint: CharSequence?): FilterResults {
|
||||||
val filterResults = FilterResults()
|
val filterResults = FilterResults()
|
||||||
if (constraint != null) {
|
if (constraint != null) {
|
||||||
resultList.clear()
|
val results = mutableListOf<SimpleContact>()
|
||||||
val searchString = constraint.toString().normalizeString()
|
val searchString = constraint.toString().normalizeString()
|
||||||
contacts.forEach {
|
contacts.forEach {
|
||||||
if (it.doesContainPhoneNumber(searchString) || it.name.contains(searchString, true)) {
|
if (it.doesContainPhoneNumber(searchString) || it.name.contains(searchString, true)) {
|
||||||
resultList.add(it)
|
results.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resultList.sortWith(compareBy { !it.name.startsWith(searchString, true) })
|
results.sortWith(compareBy { !it.name.startsWith(searchString, true) })
|
||||||
|
|
||||||
filterResults.values = resultList
|
filterResults.values = results
|
||||||
filterResults.count = resultList.size
|
filterResults.count = results.size
|
||||||
}
|
}
|
||||||
return filterResults
|
return filterResults
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
|
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
|
||||||
if ((results?.count ?: -1) > 0) {
|
if (results != null && results.count > 0) {
|
||||||
|
resultList.clear()
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
resultList.addAll(results.values as List<SimpleContact>)
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
} else {
|
} else {
|
||||||
notifyDataSetInvalidated()
|
notifyDataSetInvalidated()
|
||||||
|
|
Loading…
Reference in New Issue