gpt4 book ai didi

android - 如何在 Kotlin 中使用 ViewModelProvider

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:48 29 4
gpt4 key购买 nike

我是 Kotlin 的新手,请帮助我如何在 Kotlin 中使用 ViewModelProviders.of(this)

我的java代码是

 mFavViewModel = ViewModelProviders.of(this).get(FavouritesViewModel.class);

我无法在 kotlin 中找到 ViewModelProviders 类试过自动转换,但显示错误。谢谢。

这是我的 DataModel 类

class FavoritesDataViewModel:ViewModel{
private var mFavHelper: DatabaseHelper
private lateinit var mfav:ArrayList<Favorites>
constructor(application: Application) {
mFavHelper = DatabaseHelper(application)
}
fun getListFav():List<Favorites>{
if (mfav==null){
mfav = arrayListOf<Favorites>()
createDummyList()
loadFav()
}
val clonedFavs = arrayListOf<Favorites>()
for (i in 0 until mfav.size) {
clonedFavs.add(Favorites(mfav.get(i)))
}
return clonedFavs
}
fun createDummyList(){
addFav("https://www.journaldev.com", Date().getTime())
addFav("https://www.medium.com", Date().getTime())
addFav("https://www.reddit.com", Date().getTime())
addFav("https://www.github.com", Date().getTime())
addFav("https://www.hackerrank.com", Date().getTime())
addFav("https://www.developers.android.com", Date().getTime())

}
fun addFav(url:String,date:Long):Favorites{
val db:SQLiteDatabase = this.mFavHelper.getWritableDatabase();
val values: ContentValues = ContentValues();
values.put(DbSettings.DbEntry.COL_FAV_DATE,date)
values.put(DbSettings.DbEntry.COL_FAV_URL,url)
val id:Long = db.insertWithOnConflict(DbSettings.DbEntry.TABLE,
null,
values,SQLiteDatabase.CONFLICT_REPLACE)
db.close()
val fav:Favorites = Favorites(id,url,date)
mfav.add(fav)
return Favorites(fav)
}
fun loadFav(){
mfav.clear()
val db:SQLiteDatabase = mFavHelper.readableDatabase
val cursor:Cursor = db.query(DbSettings.DbEntry.TABLE,
arrayOf(
DbSettings.DbEntry._ID,
DbSettings.DbEntry.COL_FAV_URL,
DbSettings.DbEntry.COL_FAV_DATE
),
null, null, null, null, null);
while (cursor.moveToNext()){
val idxID:Int = cursor.getColumnIndex(DbSettings.DbEntry._ID)
val idxURL:Int = cursor.getColumnIndex(DbSettings.DbEntry.COL_FAV_URL)
val idxDate:Int = cursor.getColumnIndex(DbSettings.DbEntry.COL_FAV_DATE)
mfav.add(Favorites(cursor.getLong(idxID),cursor.getString(idxURL),cursor.getLong(idxDate)))
}
cursor.close()
db.close()
}

}

尝试使用以下代码行

var mFavViewModel = ViewModelProviders.of(activity).get(FavouritesViewModel::class.java)
implementation 'android.arch.lifecycle:extensions:1.1.1'

这是错误的:

  Caused by: java.lang.RuntimeException: Cannot create an instance of class com.nearex.mykotlinsample.viewmodel.FavoritesDataViewModel
at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:153)
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:210)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
at com.nearex.mykotlinsample.view.MainActivity.onCreate(MainActivity.kt:29)

最佳答案

对于 Java

对于 AndroidX 之前的版本

implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation "android.arch.lifecycle:viewmodel:1.1.0"

AndroidX

implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"

在最新版本中,ViewModel 可以如下声明

MyViewModel model = new ViewModelProvider(this).get(MyViewModel.class);

对于 Kotlin

对于 AndroidX 之前的版本

implementation 'android.arch.lifecycle:extensions-ktx:1.1.1'
implementation "android.arch.lifecycle:viewmodel-ktx:1.1.1"

安卓

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"

View 模型声明

val model = ViewModelProvider(activity)[MyViewModel::class.java]

请参阅以下链接以获取最新的 AndroidX dependency version

注意:对于pre-AndroidX,依赖版本不会更新

关于android - 如何在 Kotlin 中使用 ViewModelProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846870/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com