Kotlin-函数在下一行结束后运行

Kotlin- Function runs after next lines ended

已更新-问题中的代码现在有效

我正在尝试 运行 单击按钮后的功能。该函数更新一个数组,如果数组不为空,我想 运行 下一行(下一行将我转移到另一个 activity)。

我尝试在 filterPlaces 函数中打开新的 activity 但没有成功,startActivityIntent 不起作用。

这是更新数组的函数:

var places = ArrayList<Place>() //Global place array
class MainActivity : AppCompatActivity() {

    fun filterPlaces(types: ArrayList<String>, foods: ArrayList<String>, maxPrice: Int, maxProximity: Int) {
        var typesList = types
        val foodList = foods
        if (types.isEmpty()) {
            typesList = arrayListOf("Restaurant", "Hangouts")
            if (foods.isEmpty()) {
                foodList.add("Pizza")
            }
        }
        val db = FirebaseFirestore.getInstance()
        db.collection("places").get().addOnSuccessListener { result ->
            for (document in result) {
                val typeMatches = document.data["Type"].toString() in typesList
                val foodMatches = document.data["Food"].toString() in foodList
                var price = 0
                when (document.data["Price"].toString()) {
                    "Very cheap" -> price = 0
                    "Cheap" -> price = 1
                    "Average" -> price = 2
                    "Far" -> price = 3
                    "Very far" -> price = 4
                }
                val priceMatches = price <= maxPrice
                var proximity = 0
                when (document.data["Proximity"].toString()) {
                    "Very close" -> proximity = 0
                    "Close" -> proximity = 1
                    "Far" -> proximity = 2
                    "Very far" -> proximity = 3
                }
                val proximityMatches = proximity <= maxProximity
                if (typeMatches and foodMatches and priceMatches and proximityMatches) {
                    val place = Place(
                        document.data["Place"].toString(),
                        document.data["Type"].toString(),
                        document.data["Food"].toString(),
                        document.data["Price"].toString(),
                        document.data["Proximity"].toString()
                    )
                    places.add(place)
                    Log.d("name", "Place added successfully")
                }
            }

            //Openning the results activity
            if (places.isNotEmpty()) {
                val i = Intent(this, RelevantPlaces::class.java)
                val b = Bundle()
                b.putParcelableArrayList("places", places)
                i.putExtra("bundle", b)
                startActivity(i)
            }
        }
            .addOnFailureListener { exception ->
                Log.d("name", "Error getting documents.")
            }
    }

这是点击函数:

fun onSortFilterClicked(view: View) {
        if (places.isEmpty()) filterPlaces(types, foods, priceRange.progress, proximityRange.progress)
    }

我想 运行 filterPlaces 首先更新 places 数组,同时 运行 它,然后检查数组是否仍然为空,如果不是打开新的 activity。 实际发生的是它调用 filterPlaces 但不执行它,而是检查 places 数组(代码中的 if condition )并且只进入 filterPlaces 和 运行 里面有什么,导致我需要按两次按钮,只有数组有值。 我 运行 在 Android Studio 上进行此操作,我是这个 Kotlin 世界的新手,并且 android 总体上正在开发中。

有解决办法吗?是在函数内打开activity还是先把函数运行做成?

发生了什么事?

filterPlaces 中发出异步请求,这就是方法本身 returns 立即将控制传递给下一个代码块的原因。

如何解决?

将开始另一个 Activity 的代码移动到成功侦听器的范围内。更好的方法是将此代码放入一个单独的方法中,并在需要时调用它。

filterPlace 函数放在 Main Activity class 之外。移动 Main Activity class 中的函数,它起作用了。