samedi 1 août 2020

Android - " if " and "else" both are getting executed

I have come across a weird problem my if and else both are working at the same time and I don't know why is this happening.

Here is the Code

public class RoomChat extends AppCompatActivity {

    TextView textviewRoomName;
    RecyclerView recyclerView;
    DatabaseReference databaseReference, userDetails, dbmessage;
    EditText editTextMessage;
    ImageButton sendMessage;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    List<RoomChatModel> messages;
    RoomAdapter adapter;

    String roomID, senderName, roomName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_chat);

        intialiseVariables();

        Intent intent = getIntent();
        roomID = intent.getStringExtra("roomId");
        Log.d("roomId", roomID);
        databaseReference = FirebaseDatabase.getInstance().getReference("Rooms").
                child(roomID).child("GroupInfo");
        dbmessage = FirebaseDatabase.getInstance().getReference("Rooms").
                child(roomID);
        userDetails = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    roomName = snapshot.child("roomName").getValue().toString();
                    String roomCode = snapshot.child("roomCode").getValue().toString();
                    Log.d("Room Name:", roomName);
                    textviewRoomName.setText(roomName + " ( " + roomCode + " )");
                }else {
                    Toast.makeText(RoomChat.this, "Error Occured : Please Try Again Later",
                            Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
        userDetails.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                senderName = snapshot.child("username").getValue().toString();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


        sendMessage.setOnClickListener(v -> {
            sendMessage();
        });

        readMessage();
    }

So, when I execute this code it setText at textviewRoomName and also show Toast message.

Any Help or Suggestion.

Aucun commentaire:

Enregistrer un commentaire