我在Kotlin中使用数据类来显着减少我不得不写的Java代码量.
但是,在我的一个Java类中,我不知道如何在Kotlin中实现相同的结果.
我的Java类看起来有点像这样:
public class DataObject {
private int mId;
private String mName;
public DataObject(int id, String name) {
mId = id;
mName = name;
}
public DataObject(Context context, int id) {
mId = id;
Cursor cursor = ...
cursor.moveToFirst();
mName = cursor.getString(...);
cursor.close();
}
public int getId() {
return mId;
}
public String getName() {
return mName;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在Kotlin中重写它,到目前为止我有这个:
data class DataObject(val id: Int, val name: String) {
constructor(context: Context, id: Int) : this(id, fetchName(context))
private fun fetchName(context: …Run Code Online (Sandbox Code Playgroud) 我有一个Java文件有点像这样:
public class Thing {
private String property;
public Thing(String property) {
this.property = property;
}
public String getProperty() {
if (property == null) {
return "blah blah blah";
} else {
return property;
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然我的实际课程还有更多,但上面只是一个例子.
我想在Kotlin写这个,所以我从这开始:
class Thing(val property: String?)
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用官方文档和另一个Kotlin问题作为参考来实现自定义getter ,如下所示:
class Thing(property: String?) {
val property: String? = property
get() = property ?: "blah blah blah"
}
Run Code Online (Sandbox Code Playgroud)
但是,我的IDE(Android Studio)property以红色突出显示上面代码第3行的第二行,并给出了以下消息:
此处不允许使用初始化程序,因为该属性没有后备字段
为什么我会收到此错误,如何能够如上所述编写此自定义getter?
我注意到java.util.Scanner在读取大文件时使用非常慢(在我的例子中是CSV文件).
我想改变我目前正在阅读文件的方式,以提高性能.以下是我目前的情况.请注意,我正在为Android开发:
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(context.getAssets().open("MyFile.csv"));
Scanner inputStream = new Scanner(inputStreamReader);
inputStream.nextLine(); // Ignores the first line
while (inputStream.hasNext()) {
String data = inputStream.nextLine(); // Gets a whole line
String[] line = data.split(","); // Splits the line up into a string array
if (line.length > 1) {
// Do stuff, e.g:
String value = line[1];
}
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
使用Traceview,我设法发现主要的性能问题,特别是:java.util.Scanner.nextLine()和java.util.Scanner.hasNext().
我已经看过其他问题了(比如这个 …
CSV数据库与Android中的SQLite数据库相比如何?
查看StackOverflow上的其他问题,并阅读Android Developer Documentation,我看到SQLite数据库的使用频率远高于从CSV文件中读取数据.还有一些问题,用户希望将CSV文件导入SQLite数据库(例如,此问题或此问题).使用SQLite而不是CSV有优势吗?
我尝试过使用CSV和SQLite一点点,在性能方面我没有看到很大的区别,但如果我在这里错了,请纠正我.
据我所知,有不同的读取CSV文件的方法,我打开并使用BufferedReader类似的方式读取它:
BufferedReader reader =
new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
Run Code Online (Sandbox Code Playgroud)
SQLite数据库以通常的方式打开:
SQLiteDatabase db = helper.getReadableDatabase();
Run Code Online (Sandbox Code Playgroud)
我不太确定功能上的差异,虽然我假设SQLite更容易管理和过滤,这就是我问这个问题的原因.
总结一下:
SQLiteDatabase类?为了澄清,我知道CSV文件只是一个用逗号分隔值的文件,即不是作为SQL数据库的"数据库".但是,我仍然可以将CSV文件用作一种数据库,其中逗号分隔值表示不同的列,也可以通过检查特定列是否与特定值匹配来过滤.所以我问哪个更好从中读取数据.
我正在尝试将我的库的新版本上传到Bintray,但是我收到了错误.
我做的一个更改是向我的Javadoc添加自定义属性.例如:
/**
* The method does something.
*
* @param myParameter This is my parameter
* @see #anotherMethod(int)
* @attr ref R.styleable#MyLibrary_anAttribute
*/
Run Code Online (Sandbox Code Playgroud)
我添加的自定义属性标记是@attr ref在生成Javadoc HTML时显示相关的XML属性(如在Android Developer文档中).我在IDE(Android Studio)中将其添加为自定义标记,但在上传到Bintray时会导致错误.此外,我正在使用novoda bintray插件 - 这是我的一部分build.gradle.
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'
...
publish {
...
}
Run Code Online (Sandbox Code Playgroud)
所以当我在终端中运行以下命令时:
gradlew bintrayUpload -PbintrayUser=me -PbintrayKey=key -PdryRun=false
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
:mylibrary:compileDebugJavaWithJavac UP-TO-DATE
:mylibrary:mavenAndroidJavadocs
C:\Users\...\ALibraryFile.java:216: error: unknown tag: attr
* @attr ref R.styleable#MyLibrary_anAttribute
...
13 errors
:mylibrary:mavenAndroidJavadocs FAILED
FAILURE: Build failed with an exception.
* What …Run Code Online (Sandbox Code Playgroud) 我正在使用创建自定义Javadoc生成器Doclet,但我遇到了一些问题.
我正在关注官方文档,并且最初tools.jar在我的项目中包含该文件时遇到了问题,但我设法解决了这个问题.
我现在的问题是运行此命令后......
javadoc -doclet ListClass -docletpath . MyClass.java
Run Code Online (Sandbox Code Playgroud)
......我收到的消息......
javadoc:错误 - 找不到doclet类ListClass
正如我所说,我大部分时间都在关注官方文档中的教程,但这里是我的代码供参考.
ListClass.java:
import com.sun.javadoc.*;
public class ListClass {
public static boolean start(RootDoc root) {
ClassDoc[] classes = root.classes();
for (int i = 0; i < classes.length; ++i) {
System.out.println(classes[i]);
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
而且MyClass.java:
/**
* Documentation for my class
*/
public class MyClass {
public static void main(String[] args) {
}
/**
* Documentation for my …Run Code Online (Sandbox Code Playgroud) 我已经阅读了答案和博客文章,这些文章解释了Android中的VectorDrawables以及如何使用它们代替不同像素密度的PNG文件。
我已经看到有一个android:tintXML属性可以在ImageButtons和相似的Views上使用,但是我希望能够对用作菜单项的矢量图标应用色彩,因为您无法android:tint在菜单项上使用。
一篇博客文章解释说,可以像这样创建着色的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_action_something"
android:tint="@color/color_action_icons_tint"/>
Run Code Online (Sandbox Code Playgroud)
其中上面的XML文件是着色的可绘制对象,所引用的可绘制的贯穿对象src是原始矢量(黑色),并且tint是图标将被着色到的颜色。
但是,以上内容对我不起作用,给我以下错误:
android.content.res.Resources$NotFoundException: File res/drawable/ic_chevron_left_white_24dp.xml from drawable resource ID #0x7f02007e
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3735)
at android.content.res.Resources.loadDrawable(Resources.java:3603)
at android.content.res.Resources.getDrawable(Resources.java:1852)
at android.content.Context.getDrawable(Context.java:408)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:352)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at ...
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <bitmap> requires a valid src attribute
at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:761)
at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:726)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1150)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1063)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3719)
at android.content.res.Resources.loadDrawable(Resources.java:3603)
at android.content.res.Resources.getDrawable(Resources.java:1852)
at …Run Code Online (Sandbox Code Playgroud) 我一直在阅读Kotlin的房产,包括定制的吸气剂和制定者.
但是,我想知道是否可以创建一个带有额外参数的自定义getter.
例如,请考虑Java中的以下方法:
public String getDisplayedValue(Context context) {
if (PrefUtils.useImperialUnits(context)) {
// return stuff
} else {
// return other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,静态方法PrefUtils必须Context具有参数,因此不能选择删除它.
我想在Kotlin中这样写:
val displayedValue: String
get(context: Context) {
return if (PrefUtils.useImperialUnits(context)) {
// stuff
} else {
// other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
但是我的IDE用红色突出了所有这些.
我知道我可以在我的类来获得所显示的值的函数,而这将意味着我将不得不使用.getDisplayedValue(Context)在科特林的能够通过名字来引用该属性作为以及代替.displayedValue.
有没有办法创建这样的自定义getter?
编辑:如果没有,最好为此编写一个函数,或传递Context给类构造函数的参数?
我刚刚使用Udacity 上的课程、开发人员文档和GitHub 上的示例作为资源在我的应用程序中实现了 Google 登录。
我选择在我的应用程序中添加登录的原因是,用户可以在他们登录到该帐户的任何设备上访问他们的数据 - 即数据将保存在云上,可以这么说。
但是,我在开发人员文档或 Udacity 课程中找不到任何关于如何使用 Google 登录将数据保存到云的内容。
我查看了Stack Overflow 上类似问题的答案,该问题建议使用Google Drive Android API来执行此操作。
我还查看了 Firebase中的数据库,这对我来说是全新的。
所有这些让我相信我需要使用我自己的服务器来存储用户的数据(例如 Firebase,我认为,这是一项付费服务),或者我应该使用 Google Drive API。
所以,我的问题是,是否有特定的 API 可以使用用户的 Google 登录将数据保存到云中,还是我必须使用自己的服务器或 Google Drive API?
编辑:
为了澄清评论中提出的一些观点,如果我想要超过 100 个并发数据库连接(我现在不需要,但很快我可能会有 100 多个用户同时保存数据),我假设 Firebase 是其定价页面上的付费服务)。
我知道 Firebase 有自己的身份验证系统,但我没有考虑使用 Firebase,直到我意识到我需要某个地方来保存数据,这就是为什么我没有从一开始就开始使用它。
我知道 Firebase 与 Google Drive 不同,但我想知道哪个最适合我的目的。我的应用程序当前读取/写入数据到设备上的 SQLite 数据库。我想根据用户的 Google 帐户保存这些数据,以便他们可以在不同的设备上登录并拥有相同的数据。
android google-drive-android-api firebase-realtime-database google-signin
我知道在Kotlin中使用扩展函数来扩展类的功能(例如,来自库或API的功能).
但是,在代码可读性/结构方面,使用扩展函数是否有任何优势:
class Foo { ... }
fun Foo.bar() {
// Some stuff
}
Run Code Online (Sandbox Code Playgroud)
与成员函数相反:
class Foo {
...
fun bar() {
// Some stuff
}
}
Run Code Online (Sandbox Code Playgroud)
?
有推荐的做法吗?
android ×5
kotlin ×4
java ×3
csv ×2
javadoc ×2
android-menu ×1
android-xml ×1
bintray ×1
data-class ×1
doclet ×1
performance ×1