【问题标题】:how to update specific data in firebase database using android?如何使用android更新firebase数据库中的特定数据?
【发布时间】:2017-10-25 20:29:14
【问题描述】:

假设我在 firebase 数据库中有一些数据,比如 my database image

现在我想更新 trxId,其中 photoId 等于“B063527”。我无法解决这个问题,请有人帮助我。我该如何解决这个问题?提前谢谢。

【问题讨论】:

  • 你有密码吗?

标签: android firebase firebase-realtime-database


【解决方案1】:

首先,获取特定记录,然后修改其模型对象中的属性,然后将其保存。再次针对相同的 id;

private void getValue(String key){
    mdatabaseRef.child("user").orderBykey().equals(key).SingleValueEventListener(new ValueEventListener() {
    @Override     
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()){
            String key = child.getKey();
            Object object = postsnapshot.getValue(Object.class);
            updateData(object );
        }

    private updateData(Object object) {
        object.setName("test123"); 
       mDatabaseReference.child("user").child(object.getId()).setValue(object);
}

【讨论】:

    【解决方案2】:
    private void writeNewData(String userId, String name, String email) {
    User user = new User(name, email);    
    mDatabase.child("users").child(userId).setValue(user);
     }
    
     n your case you can do something like: 
        mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);
    
    If you want to update a specific value, you should be more concise: 
       mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多