我正在聊天,想知道如何爲一個對話設置一個ID。當你寫信給某人時,id是「yourId + friendId」,所以如果你的朋友想回答你,他的對話Id應該仍然是「myId + hisId」。
我必須設置databasereference子id,但在此之前,我需要檢查id是否存在「yourId + friendId」。我不知道如何。 這裏是我的代碼:如何檢查數據庫引用孩子是否存在
databaseMessages= FirebaseDatabase.getInstance().getReference("conversation");
firebaseAuth=FirebaseAuth.getInstance();
if(databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid())!=null) {
databaseMessagesChild = databaseMessages.child(chattingUser.getUserId() + ", " + firebaseAuth.getCurrentUser().getUid());
}
else {
databaseMessagesChild = databaseMessages.child(firebaseAuth.getCurrentUser().getUid() + ", " + chattingUser.getUserId());
}
它並沒有工作。 如何檢查databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid())
是否存在?
這裏是我的全部代碼:
public class Chat extends AppCompatActivity implements View.OnClickListener {
private User chattingUser;
FirebaseAuth firebaseAuth;
DatabaseReference databaseMessages, databaseMessagesChild;
ListView listViewConversation;
List<Message> messageList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_screen);
databaseMessages= FirebaseDatabase.getInstance().getReference("conversation");
firebaseAuth=FirebaseAuth.getInstance();
Intent intent=getIntent();
chattingUser= (User) intent.getSerializableExtra("User");
TextView nazwa = (TextView)findViewById(R.id.chat_with);
nazwa.setText(chattingUser.getNickname());
listViewConversation=(ListView)findViewById(R.id.list_conversation);
//listViewConversation.scrollTo(0, listViewConversation.getScrollY());
messageList=new ArrayList<>();
databaseMessagesChild = databaseMessages.child(firebaseAuth.getCurrentUser().getUid() + ", " + chattingUser.getUserId());
DatabaseReference mref=databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid());
mref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
databaseMessagesChild = databaseMessages.child(chattingUser.getUserId() + ", " + firebaseAuth.getCurrentUser().getUid());
Log.v("wybralem", "pierwszy host");
Log.v("i ten klucz to:", databaseMessagesChild.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/*
if(databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid())!=null) {
databaseMessagesChild = databaseMessages.child(chattingUser.getUserId() + ", " + firebaseAuth.getCurrentUser().getUid());
Log.v("wybralem", "pierwszy host");
}
else {
databaseMessagesChild = databaseMessages.child(firebaseAuth.getCurrentUser().getUid() + ", " + chattingUser.getUserId());
Log.v("wybralem", "drugi host");
} */
Button send = (Button)findViewById(R.id.send_button);
send.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
Log.v("KEY",databaseMessagesChild.getKey());
databaseMessagesChild.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
messageList.clear();
for(DataSnapshot usersSnapshot : dataSnapshot.getChildren()){
Message messages = usersSnapshot.getValue(Message.class);
messageList.add(messages);
}
MessagesList adapter = new MessagesList(Chat.this, messageList);
listViewConversation.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onClick(View v) {
int i = v.getId();
if(i==R.id.send_button){
EditText message_field= (EditText) findViewById(R.id.message_field);
String message_text=message_field.getText().toString();
message_field.setText("");
String myId=firebaseAuth.getCurrentUser().getUid().toString();
Message message = new Message(myId, chattingUser.getUserId() , message_text);
//Toast.makeText(Chat.this, firebaseAuth.getCurrentUser().getUid(), Toast.LENGTH_SHORT).show();
databaseMessagesChild.push().setValue(message);
}
}
}
但如何檢查它,當我開始的意圖只是一次onCrearte方法存在? 我試過你的解決方案,它每次發送消息時都會檢查。 –
你能告訴我你的oncreate()方法嗎? –
當然,我添加了整個類 –