我是Kotlin的新手,编译时得到未解决的参考
它无法引用我的课程,这是两个课程。
这是我的Activity
班
RoomActivity.kt
class RoomActivity : AppCompatActivity() {
val NEW_WORD_ACTIVITY_REQUEST_CODE = 1
// private var mWordViewModel: WordViewModel? = null
private var mWordViewModel: WordViewModel?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_room)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
val adapter = WordListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
// Get a new or existing ViewModel from the ViewModelProvider.
mWordViewModel = ViewModelProviders.of(this).get(WordViewModel::class.java)
// Add an observer on the LiveData returned by getAlphabetizedWords.
// The onChanged() method fires when the observed data changes and the activity is
// in the foreground.
mWordViewModel!!.allWords.observe(this, Observer<List<Word>> {
adapter.setWords(Word)
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
})
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
val intent = Intent(this@RoomActivity, NewWordActivity::class.java)
startActivityForResult(intent, NEW_WORD_ACTIVITY_REQUEST_CODE)
}
})
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val word = Word(data!!.getStringExtra(NewWordActivity.EXTRA_REPLY))
mWordViewModel!!.insert(word)
} else {
Toast.makeText(
applicationContext,
R.string.empty_not_saved,
Toast.LENGTH_LONG).show()
}
}
}
这是我的ViewModel
班
WordViewModel.kt
class WordViewModel(application: Application) : AndroidViewModel(application) {
private var parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
private val repository: WordRepository
val allWords: LiveData<List<Word>>
init {
val wordsDao = WordRoomDatabase.getDatabase(application)?.wordDao()
repository = WordRepository(wordsDao!!)
allWords = repository.allWords
}
fun insert(word: Word) = scope.launch(Dispatchers.IO) {
repository.insert(word)
}
override fun onCleared() {
super.onCleared()
parentJob.cancel()
}
}
那有什么问题,请帮帮我。
答案 0 :(得分:1)
如果您从另一个项目复制了文件WordViewModel.kt
,请确保将第一行中的软件包名称更改为项目中使用的软件包名称。
package application.hdlakhani.com.myapplication
答案 1 :(得分:0)
AndroidViewModel-当您需要上下文时使用该类,而在其他情况下则使用ViewModel()类。 如果您使用的是androidviewmodel或viewmodel,则以下代码均有效。
class UserViewModel : ViewModel() {
lateinit var userList: LiveData<PagedList<User>>
private val compositeDisposable = CompositeDisposable()
private val pageSize = 10
lateinit var sourceFactory: UsersDataSourceFactory
lateinit var productList:LiveData<PagedList<ProductsItem>>
var tDataSource: LiveData<ProductListSource>? = null
lateinit var productListSource:ProductDataSourceFactory
var networkState: LiveData<NetworkState>? = null
var productData: LiveData<Product>? = null
fun init(apiListener: ApiListener) {
sourceFactory = UsersDataSourceFactory(compositeDisposable, apiListener)
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
userList = LivePagedListBuilder<Long, User>(sourceFactory, config).build()
}
fun getProductData(apiListener: ApiListener){
productListSource= ProductDataSourceFactory(apiListener)
tDataSource = productListSource.mutableLiveData
networkState = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.networkState }
productData = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.productList }
val config=PagedList.Config.Builder()
.setInitialLoadSizeHint(5)
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
productList=LivePagedListBuilder<Long,ProductsItem>(productListSource,config).build()
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
fun retryGetProduct() {
productListSource.mutableLiveData.value!!.retry()
}
fun retry() {
sourceFactory.usersDataSourceLiveData.value!!.retry()
}
fun refresh() {
sourceFactory.usersDataSourceLiveData.value!!.invalidate()
}
/ * 有趣的getNetworkStateData():LiveData = Transformations.switchMap( productListSource.mutableLiveData,{it.networkState})
fun getNetworkState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.networkState })
fun getRefreshState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.initialLoad })*/
}
以这种方式访问。
viewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
答案 2 :(得分:0)