为点击侦听器拆分大方法的最佳方法

问题描述 投票:0回答:1

重建项目后,我收到一条错误消息,指出我的一种方法太大,然后将我定向到单击侦听器。有谁知道拆分或分解方法的好方法?老实说,当所有连接的字符串(RecyclerView和数组中的500+个)都与同一个项目相关时,我不知道执行此操作的推荐方法。

方法太大

e:java.lang.IllegalStateException:后端内部错误:代码生成期间的异常

        ibMap.setOnClickListener {
            when {
                tvTitle.text.toString() == "029" -> {
                    when {
                        isAppInstalled -> { // Intent to launch Google Maps if the standard package is already installed

                            val gmmIntentUri =
                                Uri.parse("geo:0,0?q=Cardiff, United Kingdom")

                            val mapIntent =
                                Intent(Intent.ACTION_VIEW, gmmIntentUri)

                            mapIntent.setPackage("com.google.android.apps.maps")

                            // Attempt to start an activity that can handle the Intent
                            mCtx.startActivity(mapIntent)
                        }
                        isLiteAppInstalled -> { // Intent to launch Google Maps Go if the lite package is already installed

                            val gmmIntentUri =
                                Uri.parse("geo:0,0?q=Cardiff, United Kingdom")

                            val mapIntent =
                                Intent(Intent.ACTION_VIEW, gmmIntentUri)

                            mapIntent.setPackage("com.google.android.apps.mapslite")

                            mCtx.startActivity(mapIntent)
                        }
                        else -> { // Intent to launch Google Maps in web browser if neither of the above apps are installed

                            val str =
                                "https://www.google.com/maps/place/Cardiff,+United+Kingdom/"

                            // Attempt to start an activity that can handle the Intent
                            mCtx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(str)))
                        }
                    }
                }

               // 500 more strings
            }
android kotlin jvm onclicklistener
1个回答
0
投票

即使在这个小样本中,您也有很多重复的代码。通常,应避免复制粘贴代码块,而应将其提取为单独的方法。首先将其添加到您的RecyclerView适配器中,或者将onclick可以到达的任何位置:

fun launchMapIntent(locationName: String) {
    // determine package to open
    val mapPkg = when {
        isAppInstalled -> "com.google.android.apps.maps"
        isLiteAppInstalled -> "com.google.android.apps.mapslite"
        else -> null
    }
    val mapIntent = if(mapPkg != null) {
        // open app
        val gmmIntentUri = Uri.parse("geo:0,0?q=$locationName")
        Intent(Intent.ACTION_VIEW, gmmIntentUri).setPackage(mapPkg)
    } else {
        // fallback to browser
        val encLoc = Uri.encode(locationName)
        val str = "https://www.google.com/maps/place/$encLoc/"
        Intent(Intent.ACTION_VIEW, Uri.parse(str))
    }
    mCtx.startActivity(mapIntent)
}

然后您可以从onclick侦听器中清除重复的代码:

ibMap.setOnClickListener {
    when(tvTitle.text.toString()) {
        "029" -> launchMapIntent("Cardiff, United Kingdom")
        "030" -> launchMapIntent("other location...")
        // other cases
    }
}

或者,如果您所有的案例都在打开地图:

ibMap.setOnClickListener {
    val locationName = when(tvTitle.text.toString()) {
        "029" -> "Cardiff, United Kingdom"
        "030" -> "other location..."
        // other cases
    }
    launchMapIntent(locationName)
}

您可以通过将Map<Int, String>设为数字作为键,并将位置名称设为值,而不用进行巨大的切换或以某种方式将其保存在数据库中,来进一步优化,等等

© www.soinside.com 2019 - 2024. All rights reserved.