2017-07-21 46 views
1
{ 
    "chat_room" : { 
    "-Kp_ldAz7_g3W4riNkJY" : { 
     "message" : "Hello!", 
     "uname" : "Praveen" 
    } 
    } 
} 

这是我的一个聊天信息火力地堡DB JSON文件填充这些,我尝试以下方法,我创建一个名为“appendChat()”来填充数据库在ListView上。如何从火力地堡数据库获取数据,并在ListView

但由于某些原因ListView控件只显示最后一条消息发送

private void appendChat(DataSnapshot dataSnapshot) { 
      final ArrayList<String> chatListArr = new ArrayList<String>(); 
      Iterator i = dataSnapshot.getChildren().iterator(); 
      while (i.hasNext()) { 
       chatListArr.add((String) ((DataSnapshot) i.next()).getValue()); 
       chatListArr.add((String) ((DataSnapshot) i.next()).getValue()); 

      } 
      ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_list_item_2,android.R.id.text1, chatListArr); 
      chatList.setAdapter(arrayAdapter); 
} 

我的ListView具有用户名和该消息的子字符串的字符串,我如何更改代码检索数据库值和在ListView填充作为

username: 
    message 

回答

2

的问题是,你正在创造新的每次收到邮件时ArrayAdapter一个品牌。这有效地删除了以前的所有消息。我建议你在onCreate()中创建一次ArrayAdapter。然后,您可以将数据添加到ArrayList并在适配器上调用notifyDataSetChanged()