📜  如何在Android中创建数据并将数据添加到Firebase Firestore?(1)

📅  最后修改于: 2023-12-03 15:24:26.710000             🧑  作者: Mango

如何在Android中创建数据并将数据添加到Firebase Firestore?

Firebase Firestore是一个NoSQL文档数据库,允许存储、同步和查询数据。一些常见的用例包括存储用户数据、日志数据、应用配置等。在这篇文章中,我们将介绍如何在Android应用程序中创建数据并将数据添加到Firebase Firestore中。

准备

在使用Firebase Firestore之前,需要通过Firebase控制台创建一个Firebase项目并将其集成到Android应用程序中。可以参考Firebase官方文档

在集成Firebase SDK之后,还需要添加以下依赖项:

implementation 'com.google.firebase:firebase-firestore:22.0.2'
创建数据模型

在将数据添加到Firestore之前,我们需要先定义数据模型。考虑一个简单的示例,我们将创建一个名为“Book”的类来表示存储在Firestore中的图书数据。该类包含以下几个属性:标题、作者、类型和描述。

public class Book {
    private String title;
    private String author;
    private String type;
    private String description;

    public Book() {}

    public Book(String title, String author, String type, String description) {
        this.title = title;
        this.author = author;
        this.type = type;
        this.description = description;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}
添加数据到Firestore

接下来,我们需要将Book对象添加到Firestore中。首先,我们将获取Firestore实例:

FirebaseFirestore db = FirebaseFirestore.getInstance();

然后,我们可以在Firestore中创建一个新文档并使用set()方法将数据添加到其中:

Book book = new Book("My Book", "John Smith", "Fiction", "A great book!");
db.collection("books").document("my_book")
    .set(book)
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w(TAG, "Error adding document", e);
        }
    });

在这个例子中,我们创建了一个名为“books”的集合,并在其中创建了一个名为“my_book”的文档。我们使用set()方法将Book对象添加到这个文档中。addOnSuccessListener()和addOnFailureListener()方法用于处理成功和失败的情况。

查询数据

现在,我们可以从Firestore中检索我们刚刚添加的数据。以下是一个检索books集合中所有文档的示例代码:

db.collection("books")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.w(TAG, "Error getting documents.", task.getException());
            }
        }
    });

在这个例子中,我们使用get()方法检索books集合中的所有文档。如果检索成功,我们可以使用for循环遍历每个文档并打印文档ID和数据。

结论

在本文中,我们学习了如何在Android应用程序中使用Firebase Firestore。我们介绍了如何创建一个数据模型、将数据添加到Firestore中并查询数据。这些是使用Firestore的基础知识,也是构建更复杂的应用程序所需的知识点。