gpt4 book ai didi

android - 如何检查数据库引用子项是否存在

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:51 25 4
gpt4 key购买 nike

我正在聊天,想知道如何为一个对话设置一个 ID。当你给某人写信时,id 是“yourId+friendId”,所以如果你的 friend 想回复你,他的 conversationId 应该仍然是“myId+hisId”。
我必须设置数据库引用子 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);


}

}

最佳答案

试试这个:

DatabaseReference mref=databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid());

mref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){

}

}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});

关于android - 如何检查数据库引用子项是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com