根据MessageList订购用户列表-Kotlin&安卓 [英] Ordering Users List based on MessageList - Kotlin & Android

查看:72
本文介绍了根据MessageList订购用户列表-Kotlin&安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按用户ID在MessageList数组中出现的顺序对用户对象列表进行排序. MessageList由与用户聊天的人的ID组成(按时间排序-使用sortwith),然后将其作为用户列表以与消息列表相同的顺序发送到用户适配器.这样可以显示用户名,个人资料图片和其他详细信息...我从firebase那里获得了用户的数据快照,按原样提供,因此需要再次订购.

I am trying to sort a list of User Objects by the order in which User IDs appear in the MessageList Array. The MessageList consists of ID's of people a user is chatting with (ordered by time - using sortwith), which is then to be sent to a User Adapter as a list of Users in the same order as the message list. This is so that username, profile image and other details can be displayed... I have learnt a datasnapshot of the Users from firebase provides as is order and hence needs to be ordered again..

我已经尝试根据MessageList对用户数组列表进行排序,但是它具有ID访问的问题(要求将其公开),并且最终输出不再是用户列表.您将如何继续展示按时间戳排序的MessageList?

I have tried to sort the User Array List basis the MessageList but it has issues with id access (asking me to make it public) and that the final output is no longer a list of users. How would you proceed to showcase a timestamp ordered MessageList?

用户数据类

class Users {
private var uid: String = ""}

MessageList数据类

class MessageList {
private var id: String = ""
private var chattimeStmp: Long = 0}

基于MessagesList创建用户列表的功能

private var userMsgAdapter: UserMsgAdapter? = null
private var mUsers: List<Users>? = null
private var usersMsgList: List<MessageList>? = null
private var firebaseUser : FirebaseUser? = null
lateinit var recycler_view_msgList : RecyclerView

private fun retrieveMessageList()
    {
        mUsers = ArrayList()
        val ref = FirebaseDatabase.getInstance().reference.child("Users")
        ref.addValueEventListener(object : ValueEventListener {

            override fun onDataChange(p0: DataSnapshot) {
                (mUsers as ArrayList).clear()

                //sorting MessageList based on timestamp
                usersMsgList?.sortedBy { it.getChattimeStmp() }

                for (dataSnapshot in p0.children) {
                    val user = dataSnapshot.getValue(Users::class.java)
                    for (eachMessageList in usersMsgList!!) {
                        if (eachMessageList.getId()
                                .equals(user!!.getUID()) && !firebaseUser?.uid.equals(
                                eachMessageList.getId()
                            )
                        ) {
                            (mUsers as ArrayList).add(user)
                        }
                    }
                }

                //Creating a map and sorting list based on the other
                val orderById = usersMsgList!!.withIndex().associate { it.value to it.index }
                val mSortedUsers = (mUsers as ArrayList<Users>).sortedBy { orderById[it.getUID()] }

                userMsgAdapter = UserMsgAdapter(context!!, mUsers as ArrayList<Users>, true)
                userMsgAdapter!!.notifyDataSetChanged()
                recycler_view_msgList.adapter = userMsgAdapter
            }

            override fun onCancelled(p0: DatabaseError) {
            }
        }
        )
    }

推荐答案

在这种情况下,最好的解决方案是不根据用户对象在消息列表中出现的顺序对用户对象进行排序,而是直接为回收站中的每个对象调用回收者视图. MessageList并在适配器中获取用户详细信息.

The best solution in this case is to not sort the User Objects based on the order they appear in the Messagelist but directly call the recycler view for each object in the MessageList and get user details in the adapter.

这篇关于根据MessageList订购用户列表-Kotlin&amp;安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆