I have a question, what value I need to pass to "init: (index: Int)->Contact"
as it expects:
The integer literal does not conform to the expected type (Int) -> TypeVariable(T)
Snippet of the function
@Composable
fun ContactList(textVal: MutableState<TextFieldValue>, navigateToProfile: (Contact) -> Unit) {
var contacts = remember { DataProvider.contactList }
var contactList = contacts.toMutableList()
var filteredContacts: MutableList<Contact>
LazyColumn(
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
) {
val searchText = textVal.value.text
filteredContacts = if (searchText.isEmpty()){
contactList
}
else{
val resultList = MutableList<Contact>(10, "init: (inded: Int)->Contact")
for (contact in contactList) {
if (contact.contains(searchText.lowercase())) {
resultList.add(contact)
}
}
resultList
}
items(filteredContacts) {
ContactListItem(contact = it, navigateToProfile)
}
}
}
Snippet of the Contact
data class Contact(
val id: Int,
val name: String,
val description: String,
val recipe: String,
val ImageId: Int = 0
)
CodePudding user response:
(index: Int)->Contact
means: a function that receives Int
and returns Contact
. But I guess this does not really solve your problem.
The problem is that you use MutableList
"constructor" (it is actually a function) which is intended to create a list with exactly 10
items. Then you need to provide these items and this is what init
function is for. What you actually need here is to create an empty list and you can do this with:
val resultList = mutableListOf<Contact>()
However, if you just need to filter some collection and create another one, it is much easier to use filter():
val resultList = contactList.filter { it.contains(searchText.lowercase()) }
If Contact.contains()
is an operator
then we can also simplify it to:
val resultList = contactList.filter { searchText.lowercase() in it }