在Alert Builder Android中单击项目时更改/更新回收器视图项目

在Alert Builder Android中单击项目时更改/更新回收器视图项目,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我想实现一种排序/筛选系统,其中: 用户单击AlertBuilder,会看到“按名称排序”和“按日期排序” 单击其中任何一个,应更新recyclerview 我尝试的是: MusicDataBase 在这里,我获得所有歌曲,并根据用户选择进行排序。 (我正在使用数据存储保存用户首选项) 存储视图模型 class StorageViewModel @ViewModelInject constructor(private val preferenceStorage: PreferenceSto

我想实现一种排序/筛选系统,其中:

  • 用户单击AlertBuilder,会看到“按名称排序”和“按日期排序”
  • 单击其中任何一个,应更新recyclerview
我尝试的是:

MusicDataBase

在这里,我获得所有歌曲,并根据用户选择进行排序。 (我正在使用数据存储保存用户首选项)

存储视图模型

class StorageViewModel @ViewModelInject constructor(private val 
preferenceStorage: PreferenceStorage) : ViewModel() {
val sortOrder = preferenceStorage.sortOrder().asLiveData()
fun changeSortOption(order: String) {
    viewModelScope.launch {
        preferenceStorage.setSortOrder(order)
    }
 }
}
我也试着打过电话

  • 当排序选项的值更改时,MusicDatabase中的allSongsAdapter.notifyDataSetChanged()
e、 g

没用

当我关闭应用程序并打开时,音乐列表的排序良好(根据上次选择的排序选项)


但它不是实时更新的(当用户单击“按名称或日期排序”时)

您可以在每次需要更新或更新时重新创建适配器 在这种情况下,您可以使用diffutil,这与重新创建整个回收器视图相比是非常有效的 或者使用
invalidate()
recycler视图,这将使其重新斜体化

在kotlin中重新创建适配器,如下所示:

private fun createAdapter() {
        val mLinearLayoutManager = GridLayoutManager(context, 2)
        mRecyclerViewMediaList.layoutManager = mLinearLayoutManager
        itemListSaved = getListFiles(
            File(
                Environment.getExternalStorageDirectory()
                    .toString() + DIRECTORY_TO_SAVE_MEDIA_NOW
            )
        )!!
          mRecyclerViewMediaList.adapter = mRecyclerViewMediaAdapte  
    }

在调用notifydatasetchanged()的地方调用。

如果您使用的是DiffUtil,那么在它的帮助下,您可以更新它。您可以提供viewmodel的ChangesToption()方法吗?我已经添加了storageViewModel代码@ShamsI。我已经尝试调用recyclerview.invalide()但不起作用,还重新创建了适配器recyclerview.adapter=allsongsAdapter。-不工作如何(代码)以及在何处使用diffUtil在单击生成器中的任何项目时刷新列表?我已完成此操作。仍然不工作(尝试使用diffutil
//Observe and show all songs
 mainViewModel.mediaItems.observe(viewLifecycleOwner){ result ->
        when(result.status){
            Status.SUCCESS -> {
                result.data?.let { songs ->
                    allSongsAdapter.songs = songs
                    allSongs = songs
                }
            }
            Status.ERROR -> Unit
            Status.LOADING -> Unit
        }
    }

//AlertBuilder, On click of sort by name or date
 private fun sortByName(){
    val sortOptionName = "name"
    storageViewModel.changeSortOption(sortOptionName)
    allSongsAdapter.notifyDataSetChanged()
}

private fun sortByDateAdded(){
    val sortOptionDate = "date"
    storageViewModel.changeSortOption(sortOptionDate)
    allSongsAdapter.notifyDataSetChanged()
}
class StorageViewModel @ViewModelInject constructor(private val 
preferenceStorage: PreferenceStorage) : ViewModel() {
val sortOrder = preferenceStorage.sortOrder().asLiveData()
fun changeSortOption(order: String) {
    viewModelScope.launch {
        preferenceStorage.setSortOrder(order)
    }
 }
}
 if (it == "name"){
 sortMusic = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
 allSongsAdapter.notifyDataSetChanged()
  }
private fun createAdapter() {
        val mLinearLayoutManager = GridLayoutManager(context, 2)
        mRecyclerViewMediaList.layoutManager = mLinearLayoutManager
        itemListSaved = getListFiles(
            File(
                Environment.getExternalStorageDirectory()
                    .toString() + DIRECTORY_TO_SAVE_MEDIA_NOW
            )
        )!!
          mRecyclerViewMediaList.adapter = mRecyclerViewMediaAdapte  
    }