在android中使用for循环从房间数据库中获取多种类型的实时数据

问题描述

我在我的一个聊天应用中实现了 ROOM 数据库。对于聊天,我使用了 XMPP 服务器。我想在循环中同时获取两种类型的数据。一旦 api 调用完成并从服务器获取消息列表,我想更新那些最新时间以及列表中是否有任何未读消息状态。

下面是api调用完成后的代码,将数据放入列表并设置在适配器上。

//This list is used for set the expandablelistview
List<GroupModelEntity> entities = response.body.string();

for (int i = 0; i < entities.size(); i++) {
    List<UserListItem> usersList = entities.get(i).getUserList();
        for (int j = 0; j < usersList.size(); j++) {
            String loginName = usersList.get(j).getLoginName();                    
            int finalJ = j;

            //---------this is used for get the last message with latest time----------

            dataRepository.getLastMessageInIndividualChat(PreferenceHelper.getInstance().getLoginName(),loginName,new SingleObserver<ChatMessagesEntity>() {
                @Override
                public void onSubscribe(disposable d) {

                }

                @Override
                public void onSuccess(ChatMessagesEntity chatMessagesEntity) {

                    usersList.get(finalJ).setChatMessagesEntity(chatMessagesEntity);
                    Collections.sort(usersList,(t1,t2) -> Long.compare((t2.getChatMessagesEntity() == null) ? 0 : t2.getChatMessagesEntity().getMsgTime(),(t1.getChatMessagesEntity() == null) ? 0 : t1.getChatMessagesEntity().getMsgTime()));
                }

                @Override
                public void onError(Throwable e) {
                    e.printstacktrace();
                }
            });


        //---------this is used for get the unread count----------

        dataRepository.getPersonalUnreadCounter(loginName.toLowerCase(),new SingleObserver<Integer>() {
                @Override
                public void onSubscribe(disposable d) {
                }

                @Override
                public void onSuccess(Integer integer) {

                    if (integer != 0) {
                        usersList.get(finalJ).setUnreadCount(integer);
                        Collections.sort(usersList,(o1,o2) -> Long.compare(o2.getUnreadCount(),o1.getUnreadCount()));
                    }
                }

                @Override
                public void onError(Throwable e) {
                    AppLog.Log(tag,"onError " + e.getMessage());
                }
        });
    }

}
//here is the issue..update after list data updated successfully.
grouplistadapter.setGroupModelList(entities,isNeedToRefreshGroupChild);
grouplistadapter.notifyDataSetChanged();

问题是当 for 循环开始时,这两个查询过程仍在运行,并且列表中的数据没有更新,但在适配器中设置了 entities 列表。我想要的是从 ROOM 数据库获取所有项目数据,然后在列表上进行更新(参见代码中的 for 循环内部)。一旦列表数据将被成功更新,然后在适配器中设置该数据。

请告诉我如何执行或设置代码以从我在循环中实现的 ROOM DB 中检索实时数据。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...