在 Android 中使用 popBackStack() 不会使用 Firebase 数据更新 android-listview

Using popBackStack() in Android does not update android-listview with Firebase data

在聊天应用程序开始时,用户会看到一个可用组列表(列表视图组),用户可以创建一个新组或单击一些可用组,然后开始写消息(列表视图消息)。函数 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();
    }
}

对于所有代码,请单击 link:“http://pastebin.com/97nR68Rm

解决方案!

加藤谢谢你的耐心和帮助。我现在找到了解决问题的方法。我在 onCreateView 方法的末尾(return 之前)调用 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); 

加藤谢谢你的耐心和帮助。我现在找到了解决问题的办法。我在我的 onCreateView 方法中调用 ReadGroupData()ReadChatMessages() 最后(在 return 之前)。正如 Kato 指出的那样 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); 

(原提问者将答案作为问题的一部分发布。我将其复制在这里作为内务处理。)