如何检查值是否相等

时间:2016-10-18 09:28:49

标签: android firebase firebase-realtime-database

我想检查值是否与另一个值匹配。我试过这段代码,但它不起作用它总是让我不匹配。有人知道我怎么能检查它的方法?谢谢你们!

DatabaseReference CheckULobby = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Lobby");
CheckULobby.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        final String getULobby = dataSnapshot.getValue(String.class);
        DatabaseReference CheckFLobby = FirebaseDatabase.getInstance().getReference().child("Users").child(post_UserID).child("Lobby");

        CheckFLobby.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String getFLobby = dataSnapshot.getValue(String.class);

                if(getFLobby == getULobby){
                    Toast.makeText(CreateLobby.this, "matches", Toast.LENGTH_SHORT).show();
                }
                else{
                     Toast.makeText(CreateLobby.this, "doesnt match", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
});

2 个答案:

答案 0 :(得分:0)

使用String方法equalsequalsIgnoreCase

if(getFLobby.equals(getULobby)){
  Toast.makeText(CreateLobby.this, "matches", Toast.LENGTH_SHORT).show();
}else{
  Toast.makeText(CreateLobby.this, "doesnt match", Toast.LENGTH_SHORT).show();
}

OR

if(getFLobby.equalsIgnoreCase(getULobby)){
  Toast.makeText(CreateLobby.this, "matches", Toast.LENGTH_SHORT).show();
}else{
  Toast.makeText(CreateLobby.this, "doesnt match", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

试试这个:

getFLobby.equals(getULobby)