我正在使用Firebase制作聊天应用程序。我已经添加了子事件onChildAdded侦听器。当我第一次打开活动时,它会检索已经正确存在的数据,但是当我在活动打开时尝试添加子节点时,应用程序将冻结。请帮忙。 这是我的活动代码:
<!DOCTYPE html>
<html>
<body>
<a href="#" onclick="main()">Wonderful</a>
<p id="main"></p>
<p id="sub"></p>
<script>
function main(){
document.getElementById("main").innerHTML =
"<a href='#' onclick='sub()'>Chapter 1</a>";
}
function sub(){
document.getElementById("sub").innerHTML =
"Johnss's";
}
</script>
</body>
</html>
这是chatAdapter和Chat类代码:
public class ChatActivity extends AppCompatActivity {
DatabaseReference ref,ref1;
TextView t;
EditText e;
ImageButton b;
String contact,user;
ArrayList<Chat> chatList;
ListView listView;
ChatAdapter chatAdapter;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatList = new ArrayList<>();
Intent i = getIntent();
contact = i.getStringExtra("contact");
user = i.getStringExtra("user");
t = findViewById(R.id.textView3);
e = findViewById(R.id.editText8);
b = findViewById(R.id.imageButton);
listView = findViewById(R.id.listView2);
t.setText(" " + contact);
ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (!dataSnapshot.exists()) {
return;
}
Toast.makeText(getApplicationContext(), "onChildAdded of ref", Toast.LENGTH_LONG).show();
while(!(dataSnapshot.hasChild("type") && dataSnapshot.hasChild("msg") && dataSnapshot.hasChild("time"))){}
chatList.add(new Chat(dataSnapshot.child("type").getValue(Integer.class), dataSnapshot.child("msg").getValue(String.class), dataSnapshot.child("time").getValue(Long.class)));
if(chatAdapter!=null)
chatAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg = e.getText().toString();
ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
ref1 = FirebaseDatabase.getInstance().getReference().child("Messages").child(contact).child(user);
ref = ref.push();
ref1 = ref1.push();
ref.child("type").setValue(1);
ref1.child("type").setValue(2);
ref.child("msg").setValue(msg);
ref1.child("msg").setValue(msg);
ref.child("time").setValue(ServerValue.TIMESTAMP);
ref1.child("time").setValue(ServerValue.TIMESTAMP);
}
});
chatAdapter = new ChatAdapter(this, chatList);
listView.setAdapter(chatAdapter);
}
}
这是我的Firebase数据的样子:
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList = new ArrayList<>();
public ContactAdapter(@NonNull Context context, ArrayList<Contact> list) {
super(context, 0 , list);
this.context = context;
contactList = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(context).inflate(R.layout.contact_item,parent,false);
final Contact currentContact = contactList.get(position);
TextView name = (TextView) listItem.findViewById(R.id.textView);
name.setText(" " + currentContact.getUser());
TextView release = (TextView) listItem.findViewById(R.id.textView2);
release.setText(" " + currentContact.getLastMsg());
return listItem;
}
}
public class Chat {
private int type;
private String msg;
private long time;
public Chat(int type,String msg,long time){this.type = type; this.msg = msg; this.time = time;}
public int getType(){return type;}
public void setType(int type){this.type = type;}
public String getMsg(){return msg;}
public void setMsg(String msg){this.msg = msg;}
public long getTime(){return time;}
public void setTime(long time){this.time = time;}
}