Firestore - 从POJO更新文档

时间:2018-04-14 07:16:05

标签: android firebase google-cloud-firestore

以下方法我可以更新我从HashMap创建的文档中的字段:

public void saveNote(View v) {
    String title = editTextTitle.getText().toString();
    String description = editTextDescription.getText().toString();

    Map<String, Object> note = new HashMap<>();
    note.put(KEY_TITLE, title);
    note.put(KEY_DESCRIPTION, description);


    noteRef.set(note)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, e.toString());
                }
            });
}

public void updateDescription(View v) {
    String description = editTextDescription.getText().toString();

    noteRef.update(KEY_DESCRIPTION, description);
}

如何从POJO保存文档并使用toObject将其转换回来时更新字段?有什么例子吗?

1 个答案:

答案 0 :(得分:0)

为了理解它,用一个简单的pojo类来解释。

这是示例pojo类

public class User { String name; String mobile; String email; String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }

**Creating/adding new Document**

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    firebaseFirestore=FirebaseFirestore.getInstance();
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            User user=new User();
            user.setName("ABCD");
            user.setAddress("Some Address");
            user.setEmail("some email");
            user.setMobile("mobilenumber");
            CollectionReference collectionReference=firebaseFirestore.collection("User");
            collectionReference.document("UserOne").set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful())
                    {
                        Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    });

}

我有一个带有单个按钮的活动,其代码如下

CollectionReference

现在观看内部点击监听器的代码。这里创建一个User对象,并使用setter为其字段添加值。

之后使用名为User的集合初始化collectionReference.document("UserOne").set(user).addOnCompleteListener()并将文档添加到该集合中   UserOne

此处文档为user in set(),对象为UserOne方法。因此,这将在User集合中添加/创建名为set()的文档。

enter image description here

  

现在如何更新现有文档的字段?

方法1 此方法将使用您在documentId方法中传递的对象值更新整个文档。您必须知道要更新的文档的mobile。 您可以通过创建这样的新对象来简单地更改字段的值。这里正在更改 User user=new User(); user.setName("ABCD"); user.setAddress("Some Address"); user.setEmail("some email"); user.setMobile("0123456789"); CollectionReference collectionReference=firebaseFirestore.collection("User"); collectionReference.document("UserOne").set(user).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show(); } } }); } }); 字段的值。

mobile

现在,这将按值0123456789更新mobile字段 enter image description here

方法2

在此方法中,您可以像这样更新pojo类的特定字段。我正在使用值55555更新 CollectionReference collectionReference=firebaseFirestore.collection("User"); collectionReference.document("UserOne").update("mobile", "55555").addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show(); } } }); 字段;

documentId

enter image description here

注意:在这里,您必须知道文档的(define (counter n loN) (define (acc loN c) (cond [(null? loN) '()] [(> c 0) (acc (cdr loN) (sub1 c))] [else loN])) ;in (let ((c (- (length loN) n))) (cond ; [(>= 0 n) #f] [(positive? c) (acc loN c)] [else #f]))) (check-equal? (counter 3 '(a b c d e)) '(c d e)) (check-equal? (counter 6 '(a b c d e)) #f) (check-equal? (counter -3 '(a b c d e)) '()) (check-equal? (counter 0 '(a b c d e)) '()) 才能更新其中一个字段

使用这两种方法,您必须能够更新文档中使用的pojo类的字段。希望这会对你有所帮助。

相关问题