如何不覆盖firebase中的子节点并查看以前的数据

时间:2017-02-01 11:50:34

标签: android firebase firebase-realtime-database

我怎么能不在我的firebase数据库中覆盖我的孩子,因为每当用户回到聊天室时,我的firebase的孩子被覆盖,导致过去的消息被删除。有人可以帮帮我吗? Thaaanks。

以下是聊天室的代码。

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(pref.getString("username", "").toString()+" chats "+tvOwnerUser.getText().toString(),"");
                root.updateChildren(map);

                Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
                intent.putExtra("room_name","Car No"+tvID123.getText().toString());
                intent.putExtra("user_name", pref.getString("username", ""));
                startActivity(intent);
            }

和聊天室

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);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);



    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("Chat");

    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);
            input_msg.setText("");
        }
    });

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

刚想到答案哈哈哈!!

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(root.child("Car No"+tvID123.getText().toString()).getRoot() == null){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(pref.getString("username", "").toString()+" chats "+tvOwnerUser.getText().toString(),"");
                root.updateChildren(map);

                Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
                intent.putExtra("room_name","Car No"+tvID123.getText().toString());
                intent.putExtra("user_name", pref.getString("username", ""));
                startActivity(intent);
            }else{
                Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
                intent.putExtra("room_name","Car No"+tvID123.getText().toString());
                intent.putExtra("user_name", pref.getString("username", ""));
                startActivity(intent);
            }
        }
    });

答案 1 :(得分:0)

检查此代码可能会对您有所帮助

        @Override
        public void onClick(View v) {
            DatabaseReference root = FirebaseDatabase.getInstance().getReference(room_name);
            final Map<String, Object> map = new HashMap<String, Object>();
            String key = root.push().getKey();
            root.updateChildren(map);

            DatabaseReference message_root = root.child(key);
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("name", "Sarvan");
            map2.put("msg", "Message");
            message_root.updateChildren(map2);
            root.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    list.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        String name = (String) snapshot.child("name").getValue();
                        String message = (String) snapshot.child("msg").getValue();
                        mapList.put("name", name);
                        mapList.put("msg", message);
                        list.add(mapList);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

}