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

时间:2017-08-03 13:09:07

标签: android firebase firebase-realtime-database firebase-authentication

我正在聊天,想知道如何为一个对话设置一个ID。当你写信给某人时,你的身份证是"你的朋友+朋友"所以,如果你的朋友想回答你,他的谈话应该仍然是" myId + hisId"。
我必须设置databasereference child 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);


    }

}

}

2 个答案:

答案 0 :(得分:3)

试试这个:

<div id="myDiv" style="width:600px;height:250px;"></div>

答案 1 :(得分:0)

databaseReference.orderByChild("id").equalTo(yourId + friendId).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // Data exists
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});