Reputation: 45
I have a list of items that display just fine in a list fragment. However, I've added a sort button to the app bar and I'm having trouble getting the resorted-data to display (or re-inflate).
This Fragment List Kotlin class successfully calls actionListViewModel.sortActions()
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.new_action -> {
val action = Action()
actionListViewModel.addAction(action)
callbacks?.onActionSelected(action.id)
true
}
R.id.sort_actions -> {
actionListViewModel.sortActions()
true
}
else -> return super.onOptionsItemSelected(item)
}
}
List View Model function:
fun sortActions(){
actionRepository.getSortedActions()
Log.d(TAG, "tried to sort")
// How do I redisplay?
}
My Repository Code:
fun getSortedActions(): LiveData<List> = actionDao.getSortedActions()
My Dao Code:
@Query("SELECT * FROM action ORDER BY title")
fun getSortedActions(): LiveData<List<Action>>
Upvotes: 0
Views: 67
Reputation: 45
I fixed it.
My updated sortActions function:
fun sortActions(){ actionRepository.getSortedActions()
if (sortedItems == true) {
actionListLiveData = actionRepository.getSortedActions()
sortedItems = false
}
else {
actionListLiveData = actionRepository.getActions()
sortedItems = true
}
}
Action Repository code:
fun getActions(): LiveData<List<Action>> = actionDao.getActions()
fun getSortedActions(): LiveData<List<Action>> = actionDao.getSortedActions()
Which of course, links to the Dao:
@Query("SELECT * FROM action ORDER BY dueDate")
fun getActions(): LiveData<List<Action>>
@Query("SELECT * FROM action ORDER BY title")
fun getSortedActions(): LiveData<List<Action>>
And in my Fragment List, I added:
fun reInflate() {
val fragTransaction: FragmentTransaction = this.fragmentManager!!.beginTransaction()
fragTransaction.detach(this)
fragTransaction.attach(this)
fragTransaction.commit()
}
Upvotes: 0