I'm confused by Kotlin lambda syntax.

At first, I have

.subscribe(
          { println(it) }
          , { println(it.message) }
          , { println("completed") }
      )

which works fine美元.

然后我将onNext移动到另一个名为GroupRecycleServiceAdapter的类,该类实现了Action1<ArrayList<Group>>.

.subscribe(
          view.adapter as GroupRecyclerViewAdapter
          , { println(it.message) }
          , { println("completed") }
      )

However, I got the error:

error

Error:(42, 17) Type mismatch: inferred type is () -> ??? but rx.functions.Action1<kotlin.Throwable!>! was expected
Error:(42, 27) Unresolved reference: it
Error:(43, 17) Type mismatch: inferred type is () -> kotlin.Unit but rx.functions.Action0! was expected

I can fix the error by changing to:

.subscribe(
          view.adapter as GroupRecyclerViewAdapter
          , Action1<kotlin.Throwable> { println(it.message) }
          , Action0 { println("completed") }
      )

Is there a way to write the lambda without specifying a type? (Action1<kotlin.Throwable>, Action0)

Note: subscribe is RxJava method

Edit 1

class GroupRecyclerViewAdapter(private val groups: MutableList<Group>,
                           private val listener: OnListFragmentInteractionListener?) :
RecyclerView.Adapter<GroupRecyclerViewAdapter.ViewHolder>(), Action1<ArrayList<Group>> {

推荐答案

view.adapter as GroupRecyclerViewAdapter part should be lambda func, not Action, since onError and onComplete also lambdas

so, to fix this try:

.subscribe(
          { (view.adapter as GroupRecyclerViewAdapter).call(it) }
          , { println(it.message) }
          , { println("completed") }
      )

with your names (replace Unit with your type)

class GroupRecyclerViewAdapter : Action1<Unit> {
    override fun call(t: Unit?) {
        print ("onNext")
    }
}

和兰巴斯

val ga = GroupRecyclerViewAdapter()
...subscribe(
    { result -> ga.call(result) },
    { error -> print ("error $error") },
    { print ("completed") })

用行动

...subscribe(
    ga,
    Action1{ error -> print ("error $error") },
    Action0{ print ("completed") })

挑一个

Kotlin相关问答推荐

Android前台服务 list —Altbeacon

Kotlin中的增广赋值语句中的难以理解的错误

我如何测试一个可组合组件没有显示,但如果它不存在也接受?

Kotlin stlib中是否有用于将列表<;对<;A,B&>;转换为对<;列表<;A&>,列表<;B&>;的函数

kotlin - 挂起简单方法调用链时可能存在冗余分支

限制通用Kotlin枚举为特定类型

如何从 Period.between() 返回的字符串中提取信息? (Kotlin )

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

Kotlin 可空泛型

循环中的每个元素都应填充行中可用的所有可用空间

JetPack Compose:添加点击持续时间

为什么在 Kotlin 中调用私有构造函数会导致错误为无法访问 是什么?

Kotlin 中二叉树的深度

JobIntentService 被销毁,当应用程序被销毁时

Kotlin 方法重载

将 Gradle 子元素与 Kotlin 多平台一起使用

如何修复 ViewPager2 中的Design assumption violated错误?

使用kotlinx协同程序测试中的类时出错

如何为 Java 调用者声明返回类型为void的 Kotlin Lambda?

我应该在哪里调用 MobileAds.initialize()?