sunny sunny - 2 months ago 29
Android Question

Fix My Firebase database Chat Application

Hello everyone im trying to make a simple chat app using google firebase db.

im facing the problem please help me.

when i want to insert new element in child .
But it's inserting into root of database

Like blow image.

enter image description here

But I want to insert Like .

enter image description here

Here is My Activity Code

public class RoomChat extends AppCompatActivity implements View.OnClickListener{

String room_name,userName;
EditText getMessage;
TextView setMe;
Button button;

DatabaseReference root ;
private String temp_key;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.chat_room);

button =(Button)findViewById(R.id.button);
button.setOnClickListener(this);

getMessage=(EditText)findViewById(R.id.editText);

setMe=(TextView)findViewById(R.id.textView);

room_name = getIntent().getStringExtra("Roomname");

userName =getIntent().getStringExtra("userName");

setTitle("Room -"+room_name);

}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
goIntoChatRoom();
break;

}
}

private void goIntoChatRoom() {

root = FirebaseDatabase.getInstance().getReference().child(room_name);

Log.i("RoomChat",root.getRef().child(room_name).toString());

Map<String,Object> map = new HashMap<String,Object>();

temp_key = root.push().getKey();

Log.i("RoomChat","tempkey :"+temp_key);

root.updateChildren(map);

DatabaseReference message = FirebaseDatabase.getInstance().getReference().child(temp_key);

Log.i("RoomChat",message.toString());


Map<String,Object> chatRoomMap2 = new HashMap<String,Object>();

chatRoomMap2.put("name",userName);

chatRoomMap2.put("message",getMessage.getText().toString());

message.updateChildren(chatRoomMap2);


}

}

Answer

try to change this piece of line :

root = FirebaseDatabase.getInstance().getReference().child(room_name);

to

root = FirebaseDatabase.getInstance().getReference(room_name);