user7142686
user7142686

Reputation:

Why can't I initialise my ArrayAdapter in Kotlin?

I want to initialise an ArrayAdapter using an array of strings in counties but I get an error message when I run my app. Here is my code:

class EnglandFragment : Fragment() {

    // Access a Cloud Firestore instance from your Activity
    val db = FirebaseFirestore.getInstance()
    lateinit var adapter : ArrayAdapter<String>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_england, container, false)

        var mContext: Context? = null
        mContext = getActivity() as CountriesActivity
        mContext.initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)
        var counties : Array<String>

        val docRef = db.collection("UKSites").document("England")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    counties = document.get("Counties") as Array<String>

                    adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, counties)


                } else {
                    Log.d("Debug", "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d("Debug", "get failed with ", exception)
            }

        return root
    }

}

I get the following error message:

None of the following functions can be called with the arguments supplied:
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, @NonNull p2: Array<(out) String!>) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, p2: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@NonNull p0: Context, p1: Int, @NonNull p2: (Mutable)List<String!>) defined in android.widget.ArrayAdapter

Upvotes: 1

Views: 1308

Answers (1)

Joaquim Ley
Joaquim Ley

Reputation: 4127

Seems like your code isn't providing the correct params, if you are using the androidx.fragment.app.Fragment you can simply call requireContext() and requireActivity()

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_england, container, false)

        (requireActivity() as CountriesActivity).initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)

        val docRef = db.collection("UKSites").document("England")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    val counties = document.get("Counties") as Array<String>

                    adapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, counties)


                } else {
                    Log.d("Debug", "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d("Debug", "get failed with ", exception)
            }

        return root
    }


Just an FYI this is very bad practice to cast and/or call methods from Fragment -> Activity

(requireActivity() as CountriesActivity).initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)

You should either initializeCustomActionBar on your activity or make it implement an interface that the fragment can then call (passing through the constructor).

But I wouldn't worry too much as it is outside of the scope of your question.

Upvotes: 1

Related Questions