我有这个片段:
class RecyclerViewAdapter internal constructor(
val clazz: Class<out RecyclerViewViewHolder>,
val layout: Int,
var dataList: MutableList<*>)
...
...
...
fun RecyclerView.getDataList() : ArrayList<*> {
return (adapter as RecyclerViewAdapter).dataList as ArrayList<*>
}
...
...
...
Run Code Online (Sandbox Code Playgroud)
然后我用它:
recyclerView.getDataList().add(Person("Lem Adane", "41 years old", 0))
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Error:(19, 31) Out-projected type 'ArrayList<*>' prohibits the use of
'public open fun add(index: Int, element: E): Unit defined in
java.util.ArrayList'
Run Code Online (Sandbox Code Playgroud) Kotlin扩展功能很棒.但是我怎么能对它们进行单元测试呢?特别是那些Android SDK提供的类(例如Context,Dialog).
我在下面提供了两个例子,如果有人可以分享我如何对它们进行单元测试,或者如果我真的想对它们进行单元测试,我是否需要以不同方式编写它们.
fun Context.getColorById(colorId: Int): Int {
if (Build.VERSION.SDK_INT >= 23)
return ContextCompat.getColor(this, colorId)
else return resources.getColor(colorId)
}
Run Code Online (Sandbox Code Playgroud)
和
fun Dialog.setupErrorDialog(body : String, onOkFunc: () -> Unit = {}): Dialog {
window.requestFeature(Window.FEATURE_NO_TITLE)
this.setContentView(R.layout.dialog_error_layout)
(findViewById(R.id.txt_body) as TextView).text = body
(findViewById(R.id.txt_header) as TextView).text = context.getString(R.string.dialog_title_error)
(findViewById(R.id.txt_okay)).setOnClickListener{
onOkFunc()
dismiss()
}
return this
}
Run Code Online (Sandbox Code Playgroud)
任何建议都会有所帮助.谢谢!
android unit-testing kotlin kotlin-extension kotlin-android-extensions
我有一些带有一些视图的布局,其中一个有id title_whalemare
import kotlinx.android.synthetic.main.controller_settings.*
import kotlinx.android.synthetic.main.view_double_text.*
class MainSettingsController : BaseMvpController<MvpView, MvpPresenter>() {
val title: TextView = title_whalemare
override fun getLayout(): Int {
return R.layout.controller_settings
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试找到它kotlin extensions,但我不能,因为我得到以下错误
controller_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title_whalemare"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?
我刚注意到这个lint错误:
调用需要API级别24(当前最小值为19)java.util.map#foreach
当我在Kotlin的MutableMap上使用扩展函数forEach时.当我写这条线时没有发生这种情况,但现在就在那里.我在其他机器上没有看到这个错误.
有人在想这个吗?在kotlin中拆分SPACE("")不起作用,我尝试使用不同的正则表达式代码,但根本不工作.
试过这个:
value.split("\\s")[0];
value.split("\\s+")[0];
value.split("\\s++")[0];
Run Code Online (Sandbox Code Playgroud)
然后我想出了解决方案 - >创建包含此函数的java常量类,并将字符串数组返回给您的kotlin类.
对于这个问题我们可以直接实现这个问题吗?
解决方案:正如@Edson Menegatti所说:
KOTLIN具体:工作
values.split("\\s".toRegex())[0]
Run Code Online (Sandbox Code Playgroud)
很多人建议这个解决方案: 不工作
values.split(" ")[0]
Run Code Online (Sandbox Code Playgroud)
但就我而言,它不起作用.
我的扩展功能在下面有什么问题
class Foo<T> {
fun <T> Foo<T>.plus(that: Foo<T>): Foo<T> = throw Exception()
init {
Foo<Int>() + Foo<String>() // A receiver of type Foo<T> is required
}
}
Run Code Online (Sandbox Code Playgroud)
更新
我想知道为什么它与常规扩展函数不同,其中T成功地被推断为Any并且想要实现相同的行为,例如T被推断为Foo <Any>
class Foo {
fun <T> T.foo(that: T): T = throw Exception()
init {
"str" foo 42
}
}
Run Code Online (Sandbox Code Playgroud) 和anko一样,你可以写这样的回调函数:
alert {
title = ""
message = ""
yesButton {
toast("Yes")
}
noButton {
toast("No")
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何创建这样的嵌套函数?我尝试像下面那样创建它,但似乎没有工作.
class Test {
fun f1(function: () -> Unit) {}
fun f2(function: () -> Unit) {}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我用扩展功能,
fun Context.temp(function: Test.() -> Unit) {
function.onSuccess() // doesn't work
}
Run Code Online (Sandbox Code Playgroud)
从活动中调用此内容:
temp {
onSuccess {
toast("Hello")
}
}
Run Code Online (Sandbox Code Playgroud)
不行.我在这里仍然缺乏一些基本概念.谁能在这里指导?
我对使用Android KTX和Anko的优点和缺点感到有些困惑.因为两个库都试图实现相同的最终目标,并且它们之间的界限变得有点模糊,在某些情况下,两个库中都可以使用相同的功能.
如果您帮助我澄清每个人的任务及其使用案例,我将非常感激.
提前致谢!
我正在尝试String使用以下函数扩展类型的枚举类,但我无法在调用站点使用它,如下所示:
fun <T: Enum<String>> Class<T>.join(skipFirst: Int = 0, skipLast: Int = 0): String {
return this.enumConstants
.drop(skipFirst)
.dropLast(skipLast)
.map { e -> e.name }
.joinToString()
}
MyStringEnum.join(1, 1);
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
kotlin-extension ×10
kotlin ×9
android ×6
anko ×2
generics ×2
android-ktx ×1
enums ×1
unit-testing ×1