我正在尝试将以下代码转换为Kotlin,并且仍然使用Java使用的一个类(Foo).进行此转换的正确方法是什么?
原始Java:
public class Foo {
public static final String C_ID = "ID";
public static final String C_NAME = "NAME";
public static final String[] VALUES = {"X", "Y", "Z"};
public static String[] getAll() {
return new String[] {C_ID, C_NAME};
}
}
public class Bar {
public void doStuff() {
String var1 = Foo.C_ID;
String[] array1 = Foo.VALUES;
String[] array2 = Foo.getAll();
}
}
Run Code Online (Sandbox Code Playgroud)
自动转换Foo到Kotlin
object Foo {
val C_ID = "ID"
val C_NAME = "NAME"
val VALUES = arrayOf("X", "Y", …Run Code Online (Sandbox Code Playgroud) 我有一大堆从REST Web服务返回的文本,我想直接写入文件.这样做最简单的方法是什么?
我写了以下函数扩展名WORKS.但我不禁想到有一种更清洁的方法可以做到这一点.
注意:我希望使用try with resources来自动关闭流和文件
fun File.copyInputStreamToFile(inputStream: InputStream) {
val buffer = ByteArray(1024)
inputStream.use { input ->
this.outputStream().use { fileOut ->
while (true) {
val length = input.read(buffer)
if (length <= 0)
break
fileOut.write(buffer, 0, length)
}
fileOut.flush()
}
}
}
Run Code Online (Sandbox Code Playgroud) 我具有以下我不希望使用的功能扩展
fun <T : View> T.setVisible(visible: Boolean) {
visibility = if (visible) View.VISIBLE else View.GONE
}
Run Code Online (Sandbox Code Playgroud)
不推荐使用:
myTextView.setVisible(true)
Run Code Online (Sandbox Code Playgroud)
新用法:
myTextView.isVisible = true
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个@Deprecated ReplaceWith表达式,它将自动修复此弃用。我尝试了以下方法,但它似乎不起作用:
@Deprecated("Use Android KTX isVisible", replaceWith = ReplaceWith("isVisible = visible", "androidx.core.view.isVisible"))
fun <T : View> T.setVisible(visible: Boolean) {
visibility = if (visible) View.VISIBLE else View.GONE
}
Run Code Online (Sandbox Code Playgroud)
当我在“ myTextView.setVisible(true)”上按Alt + ENTER并从弹出窗口中选择“用isVisible = visible替换”时,它只是删除了我已弃用的代码
是否存在可以用于自动修复代码(将函数调用更改为赋值)的ReplaceWith表达式?
我正在尝试在ListView中的项目上设置固定的进度.我只在ListView中看到一个不确定的旋转圆圈....我做错了什么?(我确定答案很简单......我只是没看到它)
对于以下适配器布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/some_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="@+id/some_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
码:
public class SomeListAdapter extends ArrayAdapter<MyItem> {
private static class ViewHolder {
TextView nameTextView;
ProgressBar valueProgressBar;
}
public BestChoiceListAdapter(Context context, List<MyItem> items) {
super(context, R.layout.list_item, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.nameTextView …Run Code Online (Sandbox Code Playgroud) 我需要使用 SQLite 的自定义构建(基于 sqlite.org 版本https://sqlite.org/android/doc/trunk/www/index.wiki),以便我可以使用自定义 FTS Tokenizer 并使用 SEE 进行加密我的数据库。
我使用以下代码创建 Room 数据库:
val mainDb = Room.databaseBuilder(context, MainDatabase::class.java, "main")
.openHelperFactory(SqliteOrgSQLiteOpenHelperFactory())
.build()
Run Code Online (Sandbox Code Playgroud)
我尝试实现自己的“SupportSQLiteOpenHelper.Factory”(SqliteOrgSQLiteOpenHelperFactory)并尝试实现所有需要的类,但是对“android.database. ”和“android.database.sqlite. ”有太多直接依赖项。所有这些依赖项似乎都需要包装在“SupportXXX”类中
示例:SupportSQLiteOpenHelper.Configuration 取决于:
支持SQLite数据库取决于:
我错过了什么吗?我处理这一切都是错误的吗?