【发布时间】:2019-10-22 22:34:48
【问题描述】:
场景是这样的:
道:
@Dao
interface TipDAO {
@Query("SELECT * FROM tip_table")
fun getAll(): LiveData<List<Tip>>
@Query("SELECT * FROM tip_table WHERE title LIKE :title")
fun findByName(title: String): Tip
@Query("SELECT * from tip_table ORDER BY title DESC")
fun getAlphabetizedTips(): LiveData<List<Tip>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(vararg tip: Tip)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(vararg tips: Tip)
@Delete
suspend fun delete(tip: Tip)
@Query("DELETE FROM tip_table")
suspend fun deleteAll()
存储库:
class TipRepository (private val tipDAO: TipDAO){
// Room executes all queries on a separate thread.
// Observed LiveData will notify the observer when the data has changed.
val allTips: LiveData<List<Tip>> = tipDAO.getAll()
// The suspend modifier tells the compiler that this must be called from a
// coroutine or another suspend function.
suspend fun insert (tip: Tip){
tipDAO.insert(tip)
}
fun getAlphabetizedTips (): LiveData<List<Tip>> {
return tipDAO.getAlphabetizedTips()
}
suspend fun delete (tip: Tip) {
tipDAO.delete(tip)
}
模型视图
class TipViewModel (application: Application): AndroidViewModel (application) {
private val repository : TipRepository
val allTips: LiveData<List<Tip>>
init {
val tipDAO = TipRoomDatabase.getDatabase(application).tipDao()
repository = TipRepository(tipDAO)
allTips = repository.allTips
}
fun insert (tip: Tip) = viewModelScope.launch {
repository.insert(tip)
}
fun delete (tip: Tip) = viewModelScope.launch {
repository.delete(tip)
}
fun getAlphabetizedTips () {
//How I can query so I can see the change ????¿¿¿¿
}
}
活动
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_how_to)
val recyclerView: RecyclerView = findViewById (R.id.content)
val adapter = TipListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.isNestedScrollingEnabled = false
tipViewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory(this.application))
.get(TipViewModel::class.java)
tipViewModel.allTips.observe(this,
Observer<List<Tip>> { tips ->
// Update the cached copy of the tips in the adapter.
tips?.let { adapter.setTips(tips)} //setTips notify the listener
})
val addButton: Button = findViewById(R.id.how_to_add_bt)
val delButton: Button = findViewById(R.id.how_to_delete_bt)
val sortButton: ImageButton = findViewById(R.id.how_to_sort_bt)
val searchBar: TextView = findViewById(R.id.how_to_search_bar)
addButton.setOnClickListener {
Toast.makeText(this,"ADD", Toast.LENGTH_SHORT).show()
intent = Intent(this, AddActivity::class.java)
startActivityForResult(intent, NEW_TIP_ACTIVITY_REQUEST_CODE)
}
delButton.setOnClickListener {
TipListAdapter.delete = !TipListAdapter.delete //changes a flag
}
sortButton.setOnClickListener {
tipViewModel.getAlphabetizedTips()
}
///more irrelevant code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (NEW_TIP_ACTIVITY_REQUEST_CODE == requestCode && resultCode == RESULT_OK && data!= null) {
data.let{
val description: String = it.getStringExtra(AddActivity.DESCRIPTION)!!
val title: String = it.getStringExtra(AddActivity.TITLE)!!
val image: String = it.getStringExtra(AddActivity.IMAGE)!!
Toast.makeText(this, "You have created a tip succesfully",Toast.LENGTH_SHORT).show()
val tip = Tip (null,title, image, description)
tipViewModel.insert(tip)
}
} else {
Toast.makeText(this,"The tip was not uploaded correctly",Toast.LENGTH_SHORT).show()
}
}
情况是,当我在 Model 中为 allTips 使用 LiveData 时,它会正确显示到 RecycleView 中。但是如果我使用MutableLiveData,它什么也不会显示。我的目标是使用MutableLiveData 执行查询,并通知观察者更改recycleview 数据。我的问题是:
- 在视图模型中,如何更改每个查询的 LiveData allTips 值?即:getAlphabetizedTips、getTip(title) 等
- 为什么 MutableLiveData 不能作为 sn-p 代码工作,如下所示?
[编辑]
MutableLiveData 的模型视图
class TipViewModel (application: Application): AndroidViewModel (application) {
private val repository : TipRepository
val allTips: MutableLiveData<List<Tip>>
init {
val tipDAO = TipRoomDatabase.getDatabase(application).tipDao()
repository = TipRepository(tipDAO)
allTips.value = repository.allTips.value
}
fun insert (tip: Tip) = viewModelScope.launch {
repository.insert(tip)
}
fun delete (tip: Tip) = viewModelScope.launch {
repository.delete(tip)
}
fun getAlphabetizedTips () {
//How I can query so I can see the change ????¿¿¿¿
allTips.post(respository.getAlphabetizedTips.value)
}
}
【问题讨论】:
-
你能说明你是如何使用
MutableLiveData的吗?我在问题中没有看到任何MutableLiveData。 -
@SanlokLee 我用 MutableLiveData 发布过,关键问题是使用实时数据时,recycleview 不会显示从 Room 检索到的数据。但如果我使用实时数据,它就会显示出来。
标签: android kotlin android-livedata android-jetpack