0
votes

I've been working on learning RecyclerViews in Kotlin and have become stuck on a specific error that I can't seem to work around:

val itemsHashSet = HashSet(category.items)

produces an error:

None of the following functions can be called with the arguments supplied. ((MutableCollection<out TypeVariable(E)!>..Collection<TypeVariable(E)!>?))   where E = TypeVariable(E) for   fun (c: (MutableCollection<out E!>..Collection<E!>?)): kotlin.collections.HashSet /* = java.util.HashSet / defined in kotlin.collections.HashSet (Int)   where E = TypeVariable(E) for   fun (initialCapacity: Int): kotlin.collections.HashSet / = java.util.HashSet */ defined in kotlin.collections.HashSet

I've tried changing the format and even writing the code in Java and then converting it in Android Studio to no avail.

Here's the Class I've been stuck on:

class CategoryManager(private val context: Context) {

    fun saveCategory(category: CategoryModel) {

        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
        val editor = sharedPreferences.edit()

        val itemsHashSet = HashSet(category.items)

        editor.putStringSet(category.name, itemsHashSet)

        editor.apply()
    }

    fun retrieveCategories (): ArrayList<CategoryModel> {

        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
        val data = sharedPreferences.all

        val categories = ArrayList<CategoryModel>()

        for (item in data) {

            val category = CategoryModel(item.key, arrayListOf(item.value.toString()))

            categories.add(category)

        }

        return categories

    }
}

I'm sorry if I'm a moron and this is obvious, but I can't find an applicable answer here that makes sense. Any help is appreciated

EDIT:

Category Model:

package com.pwneill.favoritelistapp

class CategoryModel (name: String, items: ArrayList<String>) {

     val name = name
     val items = arrayOf(items)

}
1
Can u add CategoryModel with question ?ADM
What type is category.items? You possibly can use them like category.items.toHashSet()Neo

1 Answers

0
votes

You can initialize HashSet like this:

val itemHashSet = hashSetOf(category.items)