首页 文章

Firestore - 从POJO更新文档

提问于
浏览
0

我可以通过以下方式更新从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

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

    这是示例pojo类

    User.java

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

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

    之后用名为 User 的集合初始化 CollectionReference 并通过 collectionReference.document("UserOne").set(user).addOnCompleteListener() 向该集合添加文档

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

    enter image description here

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

    Method 1 此方法将使用您在 set() 方法中传递的对象值更新整个文档 . 您必须知道要更新的文档的 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();
                        }
                    }
                });
            }
        });
    

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

    Method 2

    在此方法中,您可以像这样更新pojo类的特定字段 . 我正在更新 mobile 字段,其值为 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();
                        }
                    }
    
                });
    

    enter image description here

    Note: 在这里,您必须知道文档的 documentId 才能更新其中一个字段

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

相关问题