我正在按照Android开发者网站上的教程进行学习。这个Java示例中使用了Kotlin。我在Stack Overflow上找到了一篇帖子,讨论了一个等效的解法,但我不理解它们的答案。
原始代码如下:
// Defines the selection clause
private static final String SELECTION = Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
* Defines a variable to contain the selection value. Once you
* have the Cursor from the Contacts table, and you've selected
* the desired row, move the row's LOOKUP_KEY value into this
* variable.
*/
private lateinit var lookupKey: String
我已经对其进行了改写,具体如下:
// Defines the selection clause
private static final String SELECTION = ContactsContract.Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
* Defines a variable to contain the selection value. Once you
* have the Cursor from the Contacts table, and you've selected
* the desired row, move the row's LOOKUP_KEY value into this
* variable.
*/
private String lookupKey;
这个答案太简单了吗?还是有更复杂的Java翻译方法?
lateinit String
赋值,它就保证不会是null
。您可以在 Android Studio 的工具菜单中找到对应的 Java 代码,然后选择 Kotlin -> 显示 Kotlin 字节码 -> 反编译。 - Michael