Tag: kotlin

Kotlin – 如何正确连接字符串

一个非常基本的问题,在Kotlin中连接字符串的正确方法是什么? 在Java中,您可以使用concat()方法,例如 String a = "Hello "; String b = a.concat("World"); // b = Hello World 尽管如此, concat()函数不适用于Kotlin。 我应该使用+号吗?

如何使用Maven编译Java + Kotlin项目?

我试图编译有Kotlin类引用Java类的Maven项目。 这是我父母POM的一部分: … <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> … <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin-version}</version> <configuration> <source>${java-version}</source> <target>${java-version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.plugin.version}</version> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>process-test-sources</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <configuration> <scanForAnnotations>false</scanForAnnotations> </configuration> </plugin> 和POM的相关部分: <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> </dependency> … <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <configuration> <sourceDirs> <source>${project.basedir}/src/main/kotlin</source> […]

Kotlin清除了一个适配器

在Kotlin中清除适配器的最佳方法是什么? 我正在尝试清除回收站并更新值。 我正在使用LoaderManager,我有点陷入这种状态。

Kotlin生成评论文档

使用Java之前,我们曾经使用/**方法和一个不错的自动生成的文档才会出现。 不能用Kotlin做同样的事情。 此功能是否可用,默认打开? 我怎样才能打开它?

在Android中将Java与Kotlin结合

我在Android应用程序开发中从Java稍微转移到Kotlin,但是有些情况下我不想在Kotlin中编写代码,并希望用Java编写这些特定的案例: Kotlin删除了findViewByIds额外用法,这是省力的 我知道它现在是null-safety ,相反的Java总是触发与NullPointerException 还提供了Lambda表达式以及许多其他功能。 但是,仍然有一些我的代码不能写在Kotlin中,例如static成员non-primitive字段。 Kotlin注释实际上可以替代那些static members 。 但是喜欢Java的一些编码特性,不想离开它。 我的问题是可以将Java和Kotlin结合在一起吗?

Java与Kotlin接口声明

说我有这个Java和Kotlin接口: public interface JavaInterface { void onTest(); } interface KotlinInterface { fun onTest() } 为什么不能在没有构造函数的情况下创建Kotlin接口的实例? // this is okay val javaInterface: JavaInterface = JavaInterface { } // compile-time exception: interface does not have constructor val kotlinInterface1: KotlinInterface = KotlinInterface { } // this is okay val kotlinInterface2: KotlinInterface = object : KotlinInterface { override fun […]

kotlin中的数字是不可序列化的

我发现kotlin中的数字是不可序列化的。 第一个问题 Device.kt: package test.domain import javax.persistence.* Entity public class Device { public Id GeneratedValue var id: Long = -1 public var name: String = "" … } DeviceRestRepository.kt: package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository<Device, Long?> { public fun findByName(Param("name") name: String): […]

Kotlin序列“跳过”前N个条目

如何“跳过”kotlin序列/列表的前N个条目? 我正在寻找相当于C#LINQ“skip”的kotlin。

Eclipse – 如何将Kotlin添加到Tomcat项目 – 编译Kotlin和Java文件

我需要帮助将Kotlin编译添加到现有的eclipse java tomcat项目中。 为了记录,我在Eclipse Oxygen 4.7上安装了Kotlin插件。 我已经成功地创建并运行了基于Gradle的Spring引导Kotlin rest api demo,并且可以编译和运行“Kotlin Only Projects”并使用我的其他java库 – 真棒。 但是现在我有了Kotlin,我想用它来工作我的其他“传统”Tomcat 8项目,它们是Java J2EE – 动态WTP …我想在Kotlin中编写的新servlet。 我希望能够在我之前的任何一个包中的java src文件夹中添加一个Kotlin类,所以当我构建一个战争的时候,所有的逻辑上都在一起。 IDE似乎没问题 – 我在一个包(在tomcat项目的Java src包文件夹中)创建了Kotlin类,它在解析,导入或依赖性方面没有问题。 我在想WOW !,现在好了重启WTP中的Tomcat服务器,我就可以执行我的Kotlin Servlet了。 没有 – 404 404 404。 我检查了WEB-INF / classes构建文件夹和所有的Java类文件…猜我看到了什么? 一个名为KotlinServlet.kt的文件(即我的测试servlet的名称,源版本不是编译类版本) 所以由于某些原因,build / package / deployment没有编译* .kt文件,只是复制它(也许这是默认的WTP部署)。 我不知道这是否可以通过“添加Kotlin自然”来解决,奇怪的是没有一个日食Kotlin菜单功能可供我使用。 如果我右键点击我的项目,我没有得到Kotlin菜单选项。 Eclipse声明插件已安装。 所以我的问题是: 鉴于现有的传统WTP Tomcat servlet项目,我们可以如何识别和编译* .kt文件? 如何以及为什么我没有Kotlin菜单功能? (我下载并测试了Oxygen Java和J2EE版本 […]

Android Kotlin ImageView绑定

如何将数据绑定到kotlin中的imageview? <ImageView app:photoFilePath="@{item.photoFilePath}" android:layout_width="60dp" android:layout_height="60dp" /> 这是我的班级,我已经实施: class OrderInfoFragment : BaseDialogFragment() { private lateinit var mOrderItem: OrderItem override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val inflater = activity.layoutInflater val binding = DataBindingUtil.inflate<FragmentOrderInfoBinding>( inflater, R.layout.fragment_order_info, null, false) val view = binding.root binding.item = mOrderItem } @BindingAdapter("bind:photoFilePath") fun loadPhotoFilePath(imageView: ImageView, path: String) { } } 我已经尝试在伴侣对象中使用loadPhotoFilePath 。