我有一个这样定义的类:
@JsonClass(generateAdapter = true)
data class CurrentWeather(
@Json(name = "coord") val coordinates: Coordinates,
@Json(name = "weather") val condition: List<Condition>,
@Json(name = "base") val base: String,
@Json(name = "main") val weatherCondition: Weather,
@Json(name = "wind") val windCondition: Wind,
@Json(name = "clouds") val cloudCondition: Cloud,
@Json(name = "rain") val rainCondition: Rain,
@Json(name = "snow") val snowCondition: Snow,
@Json(name = "dt") val date: Double,
@Json(name = "sys") val sysCondition: Sys,
@Json(name = "id") val cityId: Long,
@Json(name = "name") val cityName: String,
@Json(name …Run Code Online (Sandbox Code Playgroud) 我有一个代表位图图像的 Base64 字符串。
我需要再次将该字符串转换为位图图像,以便在我的 Android 应用程序中的 ImageView 上使用它
怎么做?
假设我们有以下数据库条目:
@Entity
data class Dog(
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "breed") val breed: String?
)
Run Code Online (Sandbox Code Playgroud)
如您所见,并非每只狗都有定义的品种。现在我们要进行查询以根据品种搜索所有狗。
@Dao
interface DogDao {
@Query("SELECT * FROM dogs WHERE breed = :breed")
fun getByBreed(breed: String?): List<Dog>
}
Run Code Online (Sandbox Code Playgroud)
有时我们想搜索特定品种的狗,有时我们想搜索没有定义品种的狗。问题是在第二种情况下,上面的查询不起作用。为什么?当方法中的breed参数getByBreed(breed:)为空时,Room 会将此查询转换为如下内容:
SELECT * FROM dogs WHERE breed = NULL
不幸的是,SQLite 查询空值应该是这样的:
SELECT * FROM dogs WHERE breed IS NULL
问题是,如何定义一个接受可选参数的查询?
目前我有:
java {
withJavadocJar()
withSourcesJar()
}
Run Code Online (Sandbox Code Playgroud)
在多模块构建中模块的所有 build.gradle.kts 文件中。使用 gradle 和 groovy 我可以在根 gradle 文件中包含这个 - 但我似乎无法使用 kotlin gradle 文件来做到这一点。任何人都知道如何做到这一点?
我正在尝试将我的 gradle 文件切换到 Kotlin DSL。我的项目正在调用 API。
在build.gradle(app)我有一个函数来检索存储在另一个文件中的 api 密钥keys.properties。
在出现一些问题(例如)之后,我重写了函数以获取密钥。我在中编写了以下函数build.gradle.kts:
import import java.io.File
fun readFileLineByLineUsingForEachLine2(fileName: String): HashMap<String, String>{
val items = HashMap<String, String>()
File(fileName).forEachLine {
items[it.split("=")[0]] = it.split("=")[1]
}
return items
}
Run Code Online (Sandbox Code Playgroud)
然后我设置一个变量来保存特定键的值:
buildConfigField(String!, "API_KEY", returnMapOfKeys()["API_KEY"])
Run Code Online (Sandbox Code Playgroud)
修复了一些错误后,我遇到了以下问题:
app/build.gradle.kts:49:36: Expecting ')'
Run Code Online (Sandbox Code Playgroud)
上面带有buildConfigField.
有人知道这个错误在哪里吗?
或者有人知道如何使用 Kotlin DSL 从文件中检索密钥?
有没有办法在 documentID 之后获取文档,例如
private fun fetchCollectoionnAfterDocumentID(limit :Long){
val db = FirebaseFirestore.getInstance()
var query:Query = db.collection("questionCollection")
.startAfter("cDxXGLHlP56xnAp4RmE5") //
.orderBy("questionID", Query.Direction.DESCENDING)
.limit(limit)
query.get().addOnSuccessListener {
var questions = it.toObjects(QuestionBO::class.java)
questions.size
}
}
Run Code Online (Sandbox Code Playgroud)
我想在给定的Document ID之后获取排序的问题。我知道我可以使用DocumentSnapShot. 为了第二次或在应用程序恢复后获取,我必须将其保存DocumentSnapshot在首选项中。
可以在文档 ID 之后获取吗?
startAfter - > cDxXGLHlP56xnAp4RmE5
编辑
我知道我可以使用lastVisible DocumentSnapshot做到这一点。但我必须将lastVisible DocumentSnapshot保存在sharedPreference.
当应用程序首次启动时,从questionCollection中获取 10 个问题。下次必须在这些之后再提取 10 个问题lastVisible。因此,为了获取下一个 10,我必须将 DocumentSnapshot 对象保存在 sharedPreference 中。在看到我的数据库结构后建议我一个更好的方法。
还有一件事 questionID 与文档参考 ID 相同。
binding.seekbarConstrast.setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean){}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
mainViewModel.adjustConstrast(
ProcessUtils.seek2param(
seekBar!!.max, 0f, 4f, seekBar.progress
)
)
}
})
Run Code Online (Sandbox Code Playgroud)
这段代码在Android中,我讨厌下面的代码,太多的seekbars使无意义的代码变得非常长,我可以使用Kotlin或Java来简化侦听器的实现吗?像Android动画适配器一样,只实现我想要使用的东西。谢谢。
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean){}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
Run Code Online (Sandbox Code Playgroud) 所以我的Kotlin应用程序正在接受一个输入字符串,它应该是某种格式的日期:
fun haveFun(dateStr: String){
var formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
var formattedDate = dateStr.format(formatter)
println(formattedDate)
}
Run Code Online (Sandbox Code Playgroud)
问题是无论我发送哪个输入字符串,似乎一切都是有效的,并且没有抛出错误。
该程序对我发送的任何文本都很酷:61-Boby-2019甚至I'm not a date
从 Java 开始,我习惯于抛出一些异常或isValid方法,但我在 Kotlin 中还没有找到这样的东西
我已经按照旧教程构建了一个笔记应用程序。我想设置 AlertDialogue 框来确认用户是否真的想删除注释。
这就是我关闭应用程序所做的
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//inflate layout row.xml
var myView = layoutInflater.inflate(R.layout.row, null)
val myNote = listNotesAdapter[position]
myView.titleTv.text = myNote.nodeName
myView.descTv.text = myNote.nodeDes
//delete button click
myView.deleteBtn.setOnClickListener {
var dbManager = DbManager(this.context!!)
val selectionArgs = arrayOf(myNote.nodeID.toString())
dbManager.delete("ID=?", selectionArgs)
LoadQuery("%")
val builder = AlertDialog.Builder(this@MainActivity)
builder.setMessage("Are you sure you want to Delete?")
.setCancelable(false)
.setPositiveButton(
"Yes",
DialogInterface.OnClickListener { dialog, id -> this@MainActivity.finish() })
.setNegativeButton("No", DialogInterface.OnClickListener { dialog, id -> dialog.cancel() })
val alert = …Run Code Online (Sandbox Code Playgroud) private fun setupGeckoView() {
val runtime = GeckoRuntime.create(this) // crashes on this line
geckoSession.open(runtime)
geckoView.setSession(geckoSession)
val url = String(Base64.decode(MYURL, Base64.DEFAULT))
geckoSession.loadUri(url)
geckoSession.progressDelegate = createProgressDelegate()
geckoSession.settings.allowJavascript = true
}
Run Code Online (Sandbox Code Playgroud)
我在 onCreat() 中调用了 setUpGeckoView 方法,但是当我单击返回并重新打开应用程序时,应用程序崩溃并显示 IllegalStateException,提示“无法初始化 GeckoRuntime。它第一次工作仅在我单击返回然后再次打开应用程序时崩溃”
日志如下
Process: arholding.kargoshop.mk, PID: 16444
java.lang.RuntimeException: Unable to start activity ComponentInfo{arholding.kargoshop.mk/arholding.kargoshop.mk.SeckoActivity}: java.lang.IllegalStateException: Failed to initialize GeckoRuntime
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.IllegalStateException: Failed …Run Code Online (Sandbox Code Playgroud)