【问题标题】:Using popBackStack() in Android does not update android-listview with Firebase data在 Android 中使用 popBackStack() 不会使用 Firebase 数据更新 android-listview
【发布时间】:2015-01-26 20:38:57
【问题描述】:

在聊天应用程序的开头,用户会看到一个可用组列表(listview 组),用户可以创建一个新组或单击一些可用组,然后开始编写消息(listview 消息) . CreateNewMessage 和 CreateNewGroup 函数将信息正确推送到 firebase 当用户从带有消息的列表视图向后导航 (popBackStack()) 到 GroupFragment 时,上述场景会出现问题,这里应该向用户显示可用组的列表,但列表视图为空。 ReadGroupData() 函数不会从 firebase 读取已创建的组,并将它们插入到组列表视图中。如何做到这一点?

组片段:

  public void ReadGroupData() {
    Firebase  firebaserootRef = new Firebase("https://000.firebaseio.com");
    firebaserootRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String s) {
            if (snapshot.getValue() != null) {
                Group newGroup = new     Group((String)snapshot.child("name").getValue(),             
                (String) snapshot.child("id").getValue());

                if(!groupKeyValues.contains(newGroup.GetId())) {
                    groupKeyValues.add(newGroup.GetId());

                    AddToLstViewGroup(newGroup);
                    System.out.println("Read group data from firebase and 
                    inserted in listView");
                }
            }
        }
    });
}

 public void AddToLstViewGroup(Group newGroup) {
    groupNameList.add(newGroup);

    if(groupAdapter == null) {
        groupAdapter = new GroupAdapter(getActivity(), groupNameList);
    }

    if (lstViewGroup == null) {
        lstViewGroup = (ListView)  getView().
        findViewById(R.id.listView_group);
    }

    lstViewGroup.setOnItemClickListener(onItemClickListener);
    lstViewGroup.setOnItemLongClickListener(onItemLongClickListener);

    groupAdapter.notifyDataSetChanged();
    lstViewGroup.setAdapter(groupAdapter);
}

聊天片段:

      public void ReadChatMessages(Firebase firebaseRootRef) {
      firebaseRootRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String s) {
            if (snapshot.child(GetGroupId()).child("messages").
                getChildren() != null) {
                for (DataSnapshot c :

             snapshot.child(GetGroupId()).child("messages").getChildren()) {
                    String key = c.getKey();

                    Message newMessage = new Message();
                    newMessage.SetFrom((String) c.child("from").getValue());
                    newMessage.SetMsg((String)    

                    c.child("message").getValue());
                    newMessage.SetTime((String) c.child("time").getValue());
                    newMessage.SetId((String) c.child("id").getValue());

                    if ((!msgKeyValues.contains(key)) ||     
                      newMessage.GetFrom() != "") {
                        msgKeyValues.add(key);

                        AddToLstViewChat(newMessage);

                        //Automatic scrolls to last line in listView.
                        lstViewChat.setSelection(chatAdapter.getCount() -1);
                   }
                }
            }
        }

    public void AddToLstViewChat(Message newMessage) {
    chatMsgList.add(newMessage);

    if (chatAdapter == null) {
        chatAdapter = new ChatAdapter(getActivity(), chatMsgList);
    }

    if(IsMsgFromMe(newMessage)) {
        lstViewChat = (ListView) 

    getView().findViewById(R.id.listView_chat_message_me);
    } else {
        lstViewChat =  

     (ListView)getView().findViewById(R.id.listView_chat_message_others);
    }

    chatAdapter.notifyDataSetChanged();
    lstViewChat.setAdapter(chatAdapter);
}

聊天活动:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        finish();
    }
}

所有代码点击链接:“http://pastebin.com/97nR68Rm

解决方案!

加藤感谢您的耐心和帮助。我现在找到了解决问题的方法。我在 onCreateView 方法的最后(返回之前)调用了 ReadGroupData() 和 ReadChatMessages()。正如 Kato 指出的那样 onCreate() 没有被调用 popBackStack() 在我的 AddToLStViewGroup 中,lstViewGroup 的 if 语句被删除,所以现在它总是设置 listView 否则它将抛出一个找不到正确视图的异常,澄清一下:

删除了这一行:

if (lstViewGroup == null) {
       lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);         
}

并替换为:

ListView lstViewGroup=(ListView)getView().findViewById(R.id.listView_group); 

【问题讨论】:

  • 你能缩小范围吗?这是问题的哪一部分?这里给出的上下文似乎在说:这是一个我需要帮助调试的应用程序,当我确定这不是意图时。只选择一个问题,并查看creating an mcve
  • 感谢您的回复和反馈,我已经更新了相关问题。我希望我能缩小范围,如果还不清楚,请告诉我。
  • 看起来更好。但是“没有正确更新”并不能准确描述问题。请记住,我们没有上下文——我们从未见过你的应用程序,也没有见过你在此处发布的任何东西——所以我们看到的只是一个“未正确更新”的代码 sn-p 并且不要真的不知道那是什么意思。
  • 改进的解释。
  • 很高兴您对此进行了排序。将您的解决方案作为问题的答案发布并接受(稍等片刻)。这对其他人会有所帮助。

标签: android android-listview firebase


【解决方案1】:

加藤感谢您的耐心和帮助。我现在找到了解决问题的方法。我在 onCreateView 方法的最后(返回之前)调用ReadGroupData()ReadChatMessages()。正如加藤指出的那样,onCreate() 没有被popBackStack() 调用 在我的AddToLStViewGroup 中,listViewGroupif 语句已被删除,因此现在它始终设置 listView 否则它将因找不到正确的视图而引发异常。

澄清一下:

我删除了这一行:

if (lstViewGroup == null) {
       lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);         
}

并将其替换为:

ListView lstViewGroup =(ListView)getView().findViewById(R.id.listView_group); 

(最初的提问者将答案作为问题的一部分发布。我将其复制到这里是为了做家务。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多