gpt4 book ai didi

android - Firestore - 从 POJO 更新文档

转载 作者:行者123 更新时间:2023-11-30 00:01:36 25 4
gpt4 key购买 nike

我可以通过以下方式更新我从 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 将其转换回时,如何更新字段?有例子吗?

最佳答案

为了让它更容易理解,我用一个简单的 pojo 类来解释。

这是示例 pojo 类

用户.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;
}
}

我有一个只有一个按钮的 Activity ,它的代码如下

**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

Now How to update fields of existing document?

方法一此方法将使用您在 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 更新 mobile 字段 enter image description here

方法二

在这种方法中,您可以像这样更新 pojo 类的特定字段。正在用值 55555 更新 mobile 字段;

  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

注意:此处您必须知道文档的documentId才能更新其字段之一

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

关于android - Firestore - 从 POJO 更新文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com