使用setValue

时间:2018-05-20 00:26:57

标签: java android firebase-realtime-database

所以我在Firebase中有一个数据库。

此代码位于onCreate()

    DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");

此代码位于另一个名为onClick

的方法中
mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
                .child(userID).child(uploadID).child("Messages").getRef().push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                mAuth.getCurrentUser().getEmail()));


mDatabaseRefUser.child("Chat").child(userID)
                .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").getRef().push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                userEmail));

我想要做的是在每次调用方法时添加新值而不删除先前/现有值。

目前,每次单击并启动方法时,都会删除先前/现有值,并添加新值。但我不希望被删除。我希望这个新值也可以添加。我尝试使用getRefpush(),但我的数据仍然被覆盖。我在这里做错了什么?

这是我JSON第一次调用方法

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
          "-LCvqDidnNqQ8ALZhDud" : {
            "messageText" : "Very good",
            "messageTime" : 1526791230552,
            "messageUser" : "mama@mama.com"
          }
        },
        "chatAgainstUserEmail" : "mama2@mama.com",
        "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
        "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
        "userEmail" : "mama@mama.com",
        "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
      }
    }
  },
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
    "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
          "-LCvqDigI8ZwHDYuPhw9" : {
            "messageText" : "Very good",
            "messageTime" : 1526791230555,
            "messageUser" : "mama2@mama.com"
          }
        },
        "chatAgainstUserEmail" : "mama@mama.com",
        "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
        "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
        "userEmail" : "mama2@mama.com",
        "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

这是我JSON,当我第二次打电话时

"Users" : {
"Chat" : {
  "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
    "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
          "-LCvr0zoHOPxsKOVJzeu" : {
            "messageText" : "Very good 2nd call",
            "messageTime" : 1526791440547,
            "messageUser" : "mama@mama.com"
          }
        },
        "chatAgainstUserEmail" : "mama2@mama.com",
        "chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
        "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
        "userEmail" : "mama@mama.com",
        "userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
      }
    }
  },
  "xfqAFlRpbUZRlZb76svN5FUtbU93" : {
    "Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
      "-LCvIA_9L1Y9rjkm96Aj" : {
        "Messages" : {
          "-LCvr0zrh8wQ2WurUezs" : {
            "messageText" : "Very good 2nd call",
            "messageTime" : 1526791440550,
            "messageUser" : "mama2@mama.com"
          }
        },
        "chatAgainstUserEmail" : "mama@mama.com",
        "chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
        "uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
        "userEmail" : "mama2@mama.com",
        "userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试使用 .add

arguments must have the same length