【问题标题】:How to sort recyclerview data fetched from firebase by value in mvvm architecture如何在 mvvm 架构中按值对从 firebase 获取的 recyclerview 数据进行排序
【发布时间】:2022-12-03 22:16:29
【问题描述】:

我正在尝试根据使用 MVVM 架构的子值对从 firebase 实时数据库中获取的数据进行排序,数据库引用是在存储库中创建的

GroupNoticeRepository

class GroupNoticeRepository(private var groupSelected: String) {
    val auth = Firebase.auth
    val user = auth.currentUser!!.uid

    private val scheduleReference: DatabaseReference =
        FirebaseDatabase.getInstance().getReference("group-notice").child(groupSelected)

    @Volatile
    private var INSTANCE: GroupNoticeRepository? = null
    fun getInstance(): GroupNoticeRepository {
        return INSTANCE ?: synchronized(this) {

            val instance = GroupNoticeRepository(groupSelected)
            INSTANCE = instance
            instance
        }
    }

    fun loadSchedules(allSchedules: MutableLiveData<List<GroupNoticeData>>) {

        scheduleReference.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {

                try {

                    val scheduleList: List<GroupNoticeData> =
                        snapshot.children.map { dataSnapshot ->

                            dataSnapshot.getValue(GroupNoticeData::class.java)!!

                        }
                    allSchedules.postValue(scheduleList)

                } catch (_: Exception) {

                }

            }

            override fun onCancelled(error: DatabaseError) {

            }


        })
    }
}

GroupNoticeFragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    recycler = binding.taskList
    recycler.layoutManager = LinearLayoutManager(context)
    recycler.setHasFixedSize(true)
    adapter = GroupNoticeAdapter(_inflater)
    recycler.adapter = adapter
    viewModel = ViewModelProvider(this)[GroupNoticeViewModel::class.java]

    viewModel.initialize(groupId)

    viewModel.allSchedules.observe(viewLifecycleOwner) {
        adapter!!.updateUserList(it)
    }

}

GroupNoticeViewModel


class GroupNoticeViewModel : ViewModel() {

    private lateinit var repository: GroupNoticeRepository
    private val _allSchedules = MutableLiveData<List<GroupNoticeData>>()
    val allSchedules: LiveData<List<GroupNoticeData>> = _allSchedules

    fun initialize(groupSelected: String) {
        repository = GroupNoticeRepository(groupSelected).getInstance()
        repository.loadSchedules(_allSchedules)
    }
}

`

正如你所看到的当前结构 群通知 -groupId(组) -noticeId(通知) - 任务日期

在 group notice 下有一些组,每个组中都有一些 notices(noticeId) 。 每个通知都有一个任务日期。现在我正在尝试根据任务日期对通知进行排序,这意味着更接近今天日期的任务日期将首先在回收站视图中查看。或者给出最新任务日期的通知将首先出现在回收站视图中。

【问题讨论】:

    标签: kotlin sorting firebase-realtime-database android-recyclerview android-mvvm


    【解决方案1】:

    在 MVVM 架构中对从 Firebase 获取的 recyclerview 数据进行排序的最佳方法是使用自定义比较器。您可以定义一个 Comparator 接受两个对象(在您的例子中是两个 Firebase 数据对象)并比较它们的值。然后,您可以使用此比较器对 Firebase 数据对象列表进行排序,然后再将其用于填充 RecyclerView。

    是的,这是一个自定义比较器的示例,它根据“名称”字段比较两个 Firebase 数据对象:

    public class FirebaseDataComparator implements Comparator<FirebaseData> {
        @Override
        public int compare(FirebaseData o1, FirebaseData o2) {
            return o1.getName().compareTo(o2.getName());
        }
    }
    

    然后,您可以使用此比较器对 Firebase 数据对象列表进行排序,然后再将其用于填充 RecyclerView:

    List dataList = ... // 获取 Firebase 数据对象列表

    Collections.sort(dataList, new FirebaseDataComparator());

    // use dataList to populate RecyclerView

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2015-12-10
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2018-06-26
      相关资源
      最近更新 更多