0
我試圖在我的代碼中存儲一些數據,從我的firebase數據庫中檢索「接收者」和「發件人」字符串「room_type_1」和「room_type_2」。數據正在被檢索,但沒有被傳遞給字符串。它一直返回null。可能是什麼問題?Android中存儲的值返回null的字符串
databaseReference3.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
receiver = (String) dataSnapshot.child(user_id).child("username").getValue();
Log.v("postID2", receiver);
sender = (String) dataSnapshot.child(user_id).child("username1").getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
room_type_1 = sender + "_" + receiver;
room_type_2 = receiver + "_" + sender;
databaseReference6 = databaseReference5.child(room_type_1);
databaseReference7 = databaseReference5.child(room_type_2);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageArea.getText().toString();
if (!message.equals("")) {
databaseReference6.push().setValue(new Messages(message, sender));
databaseReference7.push().setValue(new Messages(message, sender));
}
}
});
databaseReference6.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//Messages messages = dataSnapshot.getValue(Messages.class);
String message = (String) dataSnapshot.child("message").getValue();
String userName = (String) dataSnapshot.child("sender").getValue();
if(userName.equals(sender)){
addMessageBox("You:-\n" + message, 1);
}
else{
addMessageBox(receiver + ":-\n" + message, 2);
}
}
@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) {
}
});
}