我是新的房间数据库,我想从一个数据库中提取一个int数组。我认为问题出在片段的代码上,当我想使用"stationlist"功能时获取数据库中存在的电台代码数组。输出"StationList"似乎是单位而不是数组:有人知道解决方案吗?谢谢你
这是我的实体和数据类:
@Parcelize
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)
val id: Int,
val Name: String,
val StationCode: Int
): Parcelable
这是我的刀:
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(user: User)
@Query("SELECT intArrayOf(StationCode) FROM user_table")
suspend fun stationlist() : Array
}
这是我的数据库:
@Database(entities = [User::class], version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: UserDatabase? = null
fun getDatabase(context: Context): UserDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
这是我的仓库:
class UserRepository(private val userDao: UserDao) {
val readAllData: LiveData<List<User>> = userDao.readAllData()
suspend fun addUser(user: User){
userDao.addUser(user)
}
suspend fun stationlist(){
userDao.stationlist()
}
}
这是我的Viewmodel:
class UserViewModel(application: Application): AndroidViewModel(application) {
val readAllData: LiveData<List<User>>
private val repository: UserRepository
init {
val userDao = UserDatabase.getDatabase(
application
).userDao()
repository = UserRepository(userDao)
readAllData = repository.readAllData
}
fun addUser(user: User){
viewModelScope.launch(Dispatchers.IO) {
repository.addUser(user)
}
}
fun stationlist(){
viewModelScope.launch(Dispatchers.IO) {
repository.stationlist()
}
}
}
最后这是片段的一部分:
class AddFragment : Fragment(), OnMapReadyCallback {
private lateinit var mUserViewModel: UserViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// here is all the part regarding displaying the layout and collecting data
mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
view.add_btn.setOnClickListener {
insertDataToDatabase()
}
return view
}
private fun insertDataToDatabase() {
if(inputCheck(name, stationcode)){
val user = User(
0,
name,
stationcode)
mUserViewModel.addUser(user)
// Here is the problem:
val StationList= mUserViewModel.stationlist()
}
问题在于dao方法。修改为:
@Query("SELECT StationCode FROM user_table")
suspend fun stationlist() : Array<Int>
编辑协程还有其他一些问题。在片段中,你称之为:
val StationList= mUserViewModel.stationlist()
但是这个方法不会立即返回。
fun stationlist(){
viewModelScope.launch(Dispatchers.IO) {
repository.stationlist()
}
}
你应该实现一个实时数据数组,当异步数据库查询返回时更新。以这种方式:
val stationList: MutableLiveData<Array<Int>>() = MutableLiveData()
fun stationlist(){
viewModelScope.launch(Dispatchers.IO) {
stationList.value = repository.stationlist()
}
}
在fragment中,你必须遵守list:
mUserViewModel.stationList.observe(viewLifecycleOwner) { array ->
// do stuff
}
您还必须更改存储库stationlist()
返回类型:
suspend fun stationlist(): Array<Int> {
return userDao.stationlist()
}
这些是了解协同程序、room和livedata的有用链接:
- https://developer.android.com/topic/libraries/architecture/livedata
- https://developer.android.com/codelabs/kotlin-android-training-coroutines-and-room 0