this is my first time asking question on stackoverflow.
I'll just go straight to the point, I'm currently developing an application which involve creating a chat room using firebase, I found this
https://www.youtube.com/watch?v=uX6_w6yhj4E
and decided to follow him. Everything was going smoothly until i finish the code and tries to run it. Everything functioned as expected except the send button that was suppose to display my message in the chat room. My send button didn't respond even though i tap on it. I can't seem to find the problem.
Here is my code for the chat_room:
public class Chat_Room extends AppCompatActivity {
private Button btn_send_msg;
private EditText input_msg;
private TextView chat_conversation;
private String user_name, room_name;
private DatabaseReference root;
private String temp_key;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_room);
btn_send_msg = (Button) findViewById(R.id.btn_send);
input_msg = (EditText) findViewById(R.id.msg_input);
chat_conversation = (TextView) findViewById(R.id.textview);
user_name = getIntent().getExtras().get("user_name").toString();
room_name = getIntent().getExtras().get("room_name").toString();
setTitle(" Room - "+room_name);
root = FirebaseDatabase.getInstance().getReference().child(room_name);
btn_send_msg.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
root.updateChildren(map);
DatabaseReference message_root = root.child(temp_key);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name",user_name);
map2.put("msg",input_msg.getText().toString());
//message_root.updateChildren(map2);
message_root.setValue(map2);
}
});
root.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private String chat_msg, chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot){
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.append(chat_user_name +" : "+chat_msg+"\n");
}
}
}
Let me know if I did something wrong or if the information provided was not enough, really appreciate for the help as I'm still a rookie in both android studio and firebase. Thanks in advance.