LiveData 适用于回收视图,但 MutableLiveData 不能。为什么?



场景是这样的:

道:

@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()
}
}

情况是,当我在模型中对所有提示使用LiveData时,它会正确显示在回收视图中。但是如果我使用MutableLiveData它什么也不显示。 我的目标是使用MutableLiveData来执行查询,并通知观察者更改回收视图数据。我的问题是:

  • 在视图模型中,如何更改我所做的每个查询的实时数据所有提示的值? 即:getAlphabetizedTips,getTip(title(等
  • 为什么它不能将 MutableLiveData 作为代码片段代码,如下所示?

[编辑]

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) 
}

}

尝试MediatorLiveData而不是可变

存储 库:

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 = MediatorLiveData<List<Tip>()

init {
val tipDAO = TipRoomDatabase.getDatabase(application).tipDao()
repository = TipRepository(tipDAO)
allTips.addSource(repository.allTips){
this.allTips.value = it
}
}

最新更新