Firebase更新正在创建一个除了最后一个字符

时间:2017-03-02 15:28:15

标签: android firebase firebase-realtime-database

我正在使用此代码更新Firebase上的数据,但它会生成新的数据。我尝试了很多代码,除了最后一个字符之外,它使用相同的密钥制作新代码。

我将其用作推荐的Firebase网站,但它不起作用。我创建了一个新的,下一次更新:

    Map<String, Object> childUpdate = new HashMap<>();
childUpdate.put("/masjeds/" + masjed.getId(), masjed.toMap());
reference.updateChildren(childUpdate);

并且此代码完成相同的

     final FirebaseDatabase database = FirebaseDatabase.getInstance();
    masjeds = database.getReference("masjeds");

        reference.child(masjed.getId()).setValue(masjed, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
         //   Toast.makeText(MyMasjedsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

Masjed Class是普通的java对象

public class Masjed {
private String userID;
private String id;
private String name;
private String address;
private String phone;
private boolean matloopEmam;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUserID() {
    return userID;
}

public void setUserID(String userID) {
    this.userID = userID;
}

public boolean isMatloopEmam() {
    return matloopEmam;
}

public void setMatloopEmam(boolean matloopEmam) {
    this.matloopEmam = matloopEmam;
}


public Masjed(String name, String address, String phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
}

public Masjed() {

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public Map<String, Object> toMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("name", name);
    map.put("address", address);
    map.put("phone", phone);
    map.put("id", id);
    map.put("userID", userID);
    return map;
}

}

enter image description here

id是我假设push.getkey并将其作为ID的问题 然后使用push.setValue(masjed)将使用相同的密钥 事实证明,当我使用它时,它并不总是Case的Key更改dosnt存在,因此它创建了新的答案,帮助我切斯特答案

4 个答案:

答案 0 :(得分:4)

我希望这有帮助,在我的宠物项目中使用它(它不是清洁解决方案,但它有效):

masjeds = database.getReference("masjeds");

ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
                masjedKey = dataSnapshot1.getKey(); // This is a member variable
                masjed = dataSnapshot1.getValue(Masjed.class); // This is a member variable
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};
masjeds.orderByChild("id").equalTo(masjed.getId()).addValueEventListener(listener);

//Use your Map to update each value this is just an example
masjeds.child(masjedKey).child("name").setValue("John"); //you can add event listener if you want to see if it's completed, but it works without the events

答案 1 :(得分:0)

   final FirebaseDatabase database = FirebaseDatabase.getInstance();
   masjeds = database.getReference("masjeds");

    // ** what is reference here? This must be masjeds **
    reference.child(masjed.getId()).setValue(masjed, new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
     //   Toast.makeText(MyMasjedsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

我在上面添加了我的发现作为评论,我发现这是一个嫌疑人,你可能会指出错误的参考。您将reference节点定位到masjeds节点,而 //detecting the file type BodyContentHandler handler = new BodyContentHandler(-1); Metadata metadata = new Metadata(); File inFile = new File("sitemap.xml.gz"); System.out.println(inFile.isFile()); FileInputStream inputstream = new FileInputStream(inFile); ParseContext pcontext = new ParseContext(); //Xml parser XMLParser xmlparser = new XMLParser(); xmlparser.parse(inputstream, handler, metadata, pcontext); System.out.println(pcontext.toString()); System.out.println("Contents of the document:" + handler.toString());//this one contains all contents from xml files and tags are also removed System.out.println("Metadata of the document:"); String[] metadataNames = metadata.names(); for(String name : metadataNames) { System.out.println(name + ": " + metadata.get(name)); 节点未使用。请验证它并更好地理解放入firebase的数据库。

答案 2 :(得分:0)

您可以尝试这样的方法,用HashMap的内容更新特定节点的子节点:

// get a reference to the node that you want to update
DatabaseReference masjedToUpdate = masjeds.child(masjed.getId());
// pass your hashmap to its "updateChildren" method
masjedToUpdate.updateChildren(masjed.toMap());

请注意,由于几个原因,这种方法仍然有点不标准。但它应该为你做好工作。

答案 3 :(得分:0)

使用

更新FireBase
mDatabase.child("masjed").child("the_key_of_child_you_want_to_update").setValue(masjedObject);

将正常工作,不会创建新的孩子

您可以查看My demo here