RetrlerView itemClickListener在Kotlin中

在Android 3年的经验之后,我在Kotlin写了我的第一个应用程序。 只是混淆了如何在Kotlin中使用带有RecyclerView的itemClickListener。 我已经尝试了特质的方法, public class MainActivity : ActionBarActivity() { protected override fun onCreate(savedInstanceState: Bundle?) { // set content view etc go above this line class itemClickListener : ItemClickListener { override fun onItemClick(view: View, position: Int) { Toast.makeText(this@MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show() } } val adapter = DrawerAdapter(itemClickListener()) mRecyclerView.setAdapter(adapter) } trait ItemClickListener { fun […]

Kotlin有没有复制功能?

replicate(n:Int,x:T):List<T>是一个长度为n的列表,其中x是每个元素的值。 我写了一个可变的版本复制如下: fun <T> mutableReplicate(n:Int, x:T) : MutableList<T>{ val xs = mutableListOf<T>() for (i in 1..n){ xs.add(x) } return xs } 在Kotlin中是否有任何bulid-in不可变的复制函数? 如何在Kotlin中自己写一个不可变的复制函数?

Kotlin:通过JSONArray迭代

我正在使用Kotlin和Realm编写一个Android应用程序。 我有一个JSONArray,我想遍历这个数组中的JSONObjects,以便将它们加载到一个Realm数据库类中: 领域类: import io.realm.RealmObject import io.realm.annotations.PrimaryKey import io.realm.annotations.Required open class Person( @PrimaryKey open var id: Long = 0, @Required open var name: String = "" ) : RealmObject() { } JSONArray: { "persons":[ { "id":0, "name":"Biatrix" }, { "id":1, "name":"Bill" }, { "id":2, "name":"Oren" }, { "id":3, "name":"Budd" } ] } 我已经尝试迭代,如下所示: for (item […]

Android Kotlin StringRes quantityString

好,所以这个: fun Context.quantityFromRes(id_: Int, qtt:Int, vararg format: Any) = resources.getQuantityString(id_, qtt, format) XML: <plurals name="header_view"> <item quantity="one">Oh no! You just lost %1$d Point</item> <item quantity="other">Oh no! You just lost %1$d Points</item> </plurals> 给出这个错误: "java.util.IllegalFormatConversionException: %d can't format [Ljava.lang.Object; arguments" 明显的Java修复: public class XmlPluralFormatter { private XmlPluralFormatter() { throw new IllegalStateException("You can't fuck me =("); } […]

在表达时自定义Kotlin

我对Kotlin真的很陌生,我想要的是这样的: when_assert_no_else { CONDITION0 -> { doSomething0() } CONDITION1 -> { doSomething1() } } 它的工作原理就像 when { CONDITION0 -> { doSomething0() } CONDITION1 -> { doSomething1() } else -> { throw RuntimeException() } } 后面的代码片段在我的项目中显示了很多时间,我想通过抛出一个异常来断言控制流没有到达else块。 或者,是否有可能通过给予when_assert_no_else的实现来定制关键字? 有什么想法吗? 谢谢。

IntelliJ不排序Kotlin进口

编写Java代码时,IntelliJ会自动按名称排序导入。 但是,当Kotlin成员入口时,他们仍然没有排序。 选择代码→优化导入( Ctrl + Alt + O )什么也不做。 这里是一个例子: import kotlin.platform.platformStatic import java.text.DateFormaty import org.hibernate.validator.constraints.NotEmpty as notEmpty import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty import javax.validation.constraints.NotNull as notNull import javax.validation.Valid as valid 我期待的是: import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty import org.hibernate.validator.constraints.NotEmpty as notEmpty import kotlin.platform.platformStatic import java.text.DateFormat import javax.validation.constraints.NotNull as notNull import javax.validation.Valid as valid 我正在使用IntelliJ 14.0.2与Kotlin插件(版本0.10.195)

与java和kotlin建设的Android应用程序

我使用核心Java构建了一个应用程序。 最近谷歌宣布主要语言为kotlin。 用kotlin创建更新的功能是不错的选择。 任何差异与执行时间或应用程序的大小,如果我有一些代码在java和一些代码在kotlin。 非常感谢

覆盖Kotlin数据类的getter

鉴于以下Kotlin类: data class Test(val value: Int) 如何重写Int getter,使其返回0如果值为负? 如果这是不可能的,有什么技巧可以达到合适的效果?

Kotlin扩展冲突

如果我有一个jar,在类路径上,我已经创建了一个扩展函数来说参数的String类,我有另一个jar在String上具有相同的扩展函数,Kotlin将如何解决这两个? 我认为,如果两个函数是在相同的包中定义,那么会有冲突? 但是,如果不同的包,我怎么能区分这两个扩展?

在Kotlin中枚举注释

我有一个由Gson序列化/反序列化的枚举: enum class PacketType { NONE; [SerializedName("request")] REQUEST; [SerializedName("response")] RESPONSE; [SerializedName("event")] EVENT; } 不幸的是,我注意到Gson忽略了SerializedName注解,并使用枚举值的大写名称。 我决定找出为什么序列化不能按预期工作,并发现Kotlin删除枚举值的所有注释。 我怎样才能使这些注释出现在生成的字节码?