![](/img/trans.png)
[英]Android - ViewModel, LiveData, Room and Retrofit and coroutines put together in kotlin
[英]How to handle query method's return type room using Kotlin, Coroutines, ViewModel, LiveData
如何使用 Kotlin、Coroutines、ViewModel、LiveData 處理查詢方法的返回類型房間
建築物失敗了,我收到很多指向我的 Dao 類的錯誤,錯誤是
錯誤一:
不確定如何處理查詢方法的返回類型(java.lang.Object)。 DELETE 查詢方法必須返回 void 或 int(刪除的行數)。
錯誤2:
錯誤:查詢方法參數應該是可以轉換為數據庫列的類型或包含此類類型的列表/數組。 您可以考慮為此添加一個類型適配器。 kotlin.coroutines.Continuation<? super kotlin.Unit> 延續);
錯誤 3:
錯誤:未使用的參數:延續 public abstract java.lang.Object clear(@org.jetbrains.annotations.NotNull()
錯誤 4:
錯誤:參數的類型必須是使用@Entity 注釋的類或其集合/數組。 kotlin.coroutines.Continuation<? super kotlin.Unit> 延續);
錯誤 5:
錯誤:不確定如何處理插入方法的返回類型。 公共抽象 java.lang.Object 插入(@org.jetbrains.annotations.NotNull()
**這是我的完整代碼: https ://drive.google.com/drive/folders/1qWoud5XogzkTmpa-GWxLJStfdUPSoV7r?usp=sharing
android kotlin - Coroutines Room ViewModel LiveData MainActivity.kt
package com.example.coroutine
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.activity_main.*
import java.util.UUID
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
private lateinit var model: StudentViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// make text view text scrollable
textView.movementMethod = ScrollingMovementMethod()
// initialize the student view model
model = ViewModelProvider(this).get(StudentViewModel::class.java)
// observe the students live data
model.students.observe(this, Observer { students->
textView.text = "Students(${students.size})..."
students.forEach {
textView.append("\n${it.id} | ${it.fullName} : ${it.result}")
}
}
)
btnInsert.setOnClickListener {
// generate a new student
val student = Student(
null,
UUID.randomUUID().toString(),
Random.nextInt(100)
)
// insert new student into room database
model.insert(student)
}
btnClear.setOnClickListener {
// delete all students from room student table
model.clear()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAE6FA"
tools:context=".MainActivity">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="#8DB600"
android:text="Insert"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:backgroundTint="#E52B50"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="@+id/btnInsert"
app:layout_constraintStart_toEndOf="@+id/btnInsert" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView"
style="@style/TextAppearance.MaterialComponents.Subtitle1"
android:layout_width="0dp"
android:layout_height="0dp"
android:textColor="#1B1811"
android:textStyle="bold"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnInsert"
app:layout_constraintVertical_bias="1.0"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
RoomSingleton.kt
package com.example.coroutine
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import android.content.Context
@Database(entities = [Student::class], version = 1)
abstract class RoomSingleton : RoomDatabase() {
abstract fun studentDao():StudentDao
companion object {
private var INSTANCE: RoomSingleton? = null
fun getInstance(context: Context): RoomSingleton {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
RoomSingleton::class.java,
"roomdb")
.build()
}
return INSTANCE as RoomSingleton
}
}
}
RoomDao.kt
package com.example.coroutine
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@Dao
interface StudentDao{
@Query("SELECT * FROM studentTbl ORDER BY id DESC")
fun getStudents():LiveData<List<Student>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(student:Student)
@Query("DELETE FROM studentTbl")
suspend fun clear()
}
RoomEntity.kt
package com.example.coroutine
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "studentTbl")
data class Student(
@PrimaryKey
var id:Long?,
@ColumnInfo(name = "uuid")
var fullName: String,
@ColumnInfo(name = "result")
var result:Int
)
學生視圖模型.kt
package com.example.coroutine
import androidx.lifecycle.AndroidViewModel
import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class StudentViewModel(application:Application): AndroidViewModel(application){
private val db:RoomSingleton = RoomSingleton.getInstance(application)
internal val students : LiveData<List<Student>> = db.studentDao().getStudents()
fun insert(student: Student){
viewModelScope.launch(Dispatchers.IO) {
db.studentDao().insert(student)
}
}
fun clear(){
viewModelScope.launch(Dispatchers.IO) {
db.studentDao().clear()
}
}
}
您在方法插入和刪除中的關鍵字 suspend 中的問題。 刪除后你的錯誤就消失了。
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(student:Student)
@Query("DELETE FROM studentTbl")
fun clear()
順便說一句,您的構建仍然無法成功。 要修復其他錯誤,您應該在應用 build.gradle 文件中的塊插件上添加“kotlin-android-extensions”。
plugins {
id 'com.android.application'
id 'kotlin-android-extensions'
id 'kotlin-android'
id 'kotlin-kapt'
}
但是此解決方案已棄用,您應該改用 viewBinding。 看一下這個。 https://developer.android.com/topic/libraries/view-binding/migration
復制您的代碼,禁止 gradle 文件,並對主要活動進行輕微調整。 編譯正常並運行插入和清除按鈕似乎工作正常。
因此,要么您構建的 gradle 不正確,要么您需要對項目進行清理或重建。
例如:-
我使用的 MainActivity 將視圖聲明為 lateinit 的,而 findViewById 的則晚了:-
class MainActivity : AppCompatActivity() {
private lateinit var model: StudentViewModel
lateinit var textView: TextView
lateinit var btnClear: Button
lateinit var btnInsert: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = this.findViewById(R.id.textView)
btnClear = this.findViewById(R.id.btnClear)
btnInsert = this.findViewById(R.id.btnInsert)
// make text view text scrollable
textView.movementMethod = ScrollingMovementMethod()
// initialize the student view model
model = ViewModelProvider(this).get(StudentViewModel::class.java)
// observe the students live data
model.students.observe(this, Observer { students->
textView.text = "Students(${students.size})..."
students.forEach {
textView.append("\n${it.id} | ${it.fullName} : ${it.result}")
}
}
)
btnInsert.setOnClickListener {
// generate a new student
val student = Student(
null,
UUID.randomUUID().toString(),
Random.nextInt(100)
)
// insert new student into room database
model.insert(student)
}
btnClear.setOnClickListener {
// delete all students from room student table
model.clear()
}
}
}
使用的構建 Gradle(模塊):-
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "a.a.so69998391kotlinroom_to_handle_query_return_types"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.room:room-ktx:2.4.0-beta01'
implementation 'androidx.room:room-runtime:2.4.0-beta01'
implementation 'androidx.lifecycle:lifecycle-common:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
kapt 'androidx.lifecycle:lifecycle-compiler:2.4.0'
kapt 'androidx.room:room-compiler:2.4.0-beta01'
}
在 Dao 函數中使用協程時我遇到了同樣的問題
對我來說只是改變
kapt "androidx.room:room-compiler:$room_version"
至
annotationProcessor "androidx.room:room-compiler:$room_version"
如果您使用List or MutableLiveData
檢查此答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.