在聊天应用程序开始时,用户看到可用的列表组(listview组),用户可以创建一个新组或单击可用组中的一些组,然后开始编写消息(listview消息)。上面的函数CreateNewMessage和CreateNewGroup正确地将信息推送到防火墙,当用户从listview向GroupFragment反向导航(popBackStack())时,会产生很好的问题,如果用户被显示出可用组的列表,但是listview是空的。ReadGroupData()函数不是从firebase读取已经创建的组,而是将它们插入到组列表视图中。怎样才能做到这一点?
GroupFragment:
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:
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:
@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,否则它会抛出一个异常,因为找不到正确的视图,以便澄清:
删除这一行:
if (lstViewGroup == null) {
lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);
}改为:
ListView lstViewGroup=(ListView)getView().findViewById(R.id.listView_group); 发布于 2015-02-12 23:14:42
加藤感谢你的耐心和帮助。我现在已经找到了解决这个问题的办法。我在我的ReadGroupData()方法的末尾(返回前)调用ReadChatMessages()和onCreateView。正如加藤指出的那样,在我的onCreate()中,popBackStack()没有被调用,listViewGroup的if语句被删除了,所以现在它总是设置listView,否则它会抛出一个异常,因为找不到正确的视图。
澄清:
我删除了这一行:
if (lstViewGroup == null) {
lstViewGroup = (ListView)getView().findViewById(R.id.listView_group);
}并代之以:
ListView lstViewGroup =(ListView)getView().findViewById(R.id.listView_group); (最初的提问者将答案作为问题的一部分。我把它复制到这里来做家务。)
https://stackoverflow.com/questions/28158334
复制相似问题