修复我的Firebase数据库聊天应用程序

时间:2016-09-10 07:49:42

标签: android firebase firebase-realtime-database

大家好我试图使用google firebase db创建一个简单的聊天应用。

即时面对这个问题,请帮助我。

当我想在child中插入新元素时。 但它已插入数据库的根目录

喜欢吹像。

enter image description here

但我想插入Like。

enter image description here

这是我的活动代码

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);


    }

}

1 个答案:

答案 0 :(得分:5)

尝试改变这条线:

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

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