为什么我不能在Kotlin中初始化ArrayAdapter?

时间:2019-12-06 18:05:53

标签: android-studio kotlin android-arrayadapter

我想使用ArrayAdapter中的字符串数组初始化counties,但是在运行应用程序时收到错误消息。这是我的代码:

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
    }

}

我收到以下错误消息:

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

1 个答案:

答案 0 :(得分:0)

似乎您的代码未提供正确的参数,如果您使用的是androidx.fragment.app.Fragment,则可以简单地调用requireContext()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
    }


仅供参考,这是从Fragment-> Activity投射和/或调用方法的非常不好的做法

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

您应该在活动中使用initializeCustomActionBar或使其实现该片段随后可以调用的接口(通过构造函数)。

但是我不会太担心,因为它超出了您的问题范围。