【问题标题】:I am retreiving data from firebase and the recyclerview is showing it out of order我正在从 firebase 检索数据,而 recyclerview 显示它的顺序不正确
【发布时间】:2019-12-01 02:16:20
【问题描述】:

我正在尝试从 firebase 检索数据。所有数据都正确显示,但出现故障,我不知道为什么?数据应该从头到尾读取:

  val publicFeedMap = HashMap<String, Feed>()

    private  fun refreshRecyclerViewMessages(){

        adapter.clear()
        publicFeedMap.values.forEach{
            adapter.add(PublicFeedItem(it))
        }
    }

    private fun fetchPublicFeed(){
        val ref = FirebaseDatabase.getInstance().getReference("PublicFeed")

        ref.addChildEventListener(object: ChildEventListener{

            override fun onChildAdded(p0: DataSnapshot, p1: String?) {
                p0.children.forEach {
                    val feedItem = p0.getValue(Feed::class.java) ?: return

                    publicFeedMap[p0.key!!] = feedItem
                    refreshRecyclerViewMessages()

                }

            }

            override fun onChildChanged(p0: DataSnapshot, p1: String?) {

            }

            override fun onChildMoved(p0: DataSnapshot, p1: String?) {
            }
            override fun onCancelled(p0: DatabaseError) {

            }
            override fun onChildRemoved(p0: DataSnapshot) {

            }
        })
    }

the data should be sorted from beginning to end

【问题讨论】:

  • 你没有提供你的数据,或者你的预期输出是什么,所以很难判断一个“正确”的顺序是什么。但是,您实际上并没有拨打任何会订购您的查询的电话(例如orderByChildorderByKey 等...)请参阅the documentation about sorting

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


【解决方案1】:

您必须通过添加以下内容来对数据进行排序: orderByKey 或 orderByChild 如果您提供您想要使用的数据以及您想要如何订购它会更好

检查这个: https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/

也检查一下: https://firebase.google.com/docs/database/web/read-and-write

【讨论】:

  • 也使用 LinkedHashMap。常规 HashMap 不会保留顺序。
猜你喜欢
  • 2018-10-02
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多