首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中使用popBackStack()不会使用Firebase数据更新android-listview。

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

Stack Overflow用户
提问于 2015-01-26 20:38:57
回答 1查看 570关注 0票数 0

在聊天应用程序开始时,用户看到可用的列表组(listview组),用户可以创建一个新组或单击可用组中的一些组,然后开始编写消息(listview消息)。上面的函数CreateNewMessage和CreateNewGroup正确地将信息推送到防火墙,当用户从listview向GroupFragment反向导航(popBackStack())时,会产生很好的问题,如果用户被显示出可用组的列表,但是listview是空的。ReadGroupData()函数不是从firebase读取已经创建的组,而是将它们插入到组列表视图中。怎样才能做到这一点?

GroupFragment:

代码语言:javascript
复制
  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);
}

ChatFragment:

代码语言:javascript
复制
      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);
}

ChatActivity:

代码语言:javascript
复制
@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        finish();
    }
}

对于所有代码,单击链接:"http://pastebin.com/97nR68Rm

解决办法!

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

删除这一行:

代码语言:javascript
复制
if (lstViewGroup == null) {
       lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);         
}

改为:

代码语言:javascript
复制
ListView lstViewGroup=(ListView)getView().findViewById(R.id.listView_group); 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 23:14:42

加藤感谢你的耐心和帮助。我现在已经找到了解决这个问题的办法。我在我的ReadGroupData()方法的末尾(返回前)调用ReadChatMessages()和onCreateView。正如加藤指出的那样,在我的onCreate()中,popBackStack()没有被调用,listViewGroupif语句被删除了,所以现在它总是设置listView,否则它会抛出一个异常,因为找不到正确的视图。

澄清:

我删除了这一行:

代码语言:javascript
复制
if (lstViewGroup == null) {
       lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);         
}

并代之以:

代码语言:javascript
复制
ListView lstViewGroup =(ListView)getView().findViewById(R.id.listView_group); 

(最初的提问者将答案作为问题的一部分。我把它复制到这里来做家务。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28158334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档