public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseUser = mAuth.getCurrentUser();
database = FirebaseDatabase.getInstance();
connectReference = database.getReference();
myBase = mDatabase.child("chat");
buttonSend = findViewById(R.id.send_btn);
mMessagesRecycler = findViewById(R.id.chat);
mMessagesRecycler.setLayoutManager(new LinearLayoutManager(this));
final ChatAdapter dataAdapter = new ChatAdapter(result);
mMessagesRecycler.setAdapter(dataAdapter);
buttonSend.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
sendText = findViewById(R.id.message_input);
FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(new ChatModel(
sendText.getText().toString(), "Админ"));
sendText.setText("");
}
});
myBase.addChildEventListener(new ChildEventListener() {
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
result.add(dataSnapshot.getValue(ChatModel.class));
dataAdapter.notifyDataSetChanged();
mMessagesRecycler.smoothScrollToPosition(result.size());
}
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}