简体   繁体   English

如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据?

[英]How to display data from Firestore in a RecyclerView with Android?

What is the best way to display data from an existing Firestore database in a RecyclerView using Android?使用 Android 在RecyclerView中显示来自现有 Firestore 数据库的数据的最佳方式是什么?

This isn't covered as a full explanation in an answer, so I've added this Q&A-style so it can be linked to in comments.答案中没有包含完整的解释,因此我添加了这种问答式,以便可以在评论中链接到它。

Assuming you have a Firestore database structure that looks like this:假设您有一个如下所示的 Firestore 数据库结构:

Firestore-root
    |
    --- products (collection)
           |
           --- documentIdOne (document)
           |        |
           |        --- productName: "Milk"
           |
           --- documentIdTwo (document)
           |        |
           |        --- productName: "Soy Milk"
           |
           --- documentIdThree (document)
                    |
                    --- productName: "Bacon"

A model class that looks also like this:一个看起来也像这样的模型类:

public class ProductModel {
    private String productName;

    public ProductModel() {}

    public ProductModel(String productName) {this.productName = productName;}

    public String getProductName() {return productName;}
}

And a .XML file that contains a RecyclerView which also looks like this:还有一个包含RecyclerView.XML文件,它也如下所示:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>

To display all the product names, please follow the next steps.要显示所有产品名称,请按照以下步骤操作。

First, you need to find the RecyclerView in your activity and set the LinearLayoutManager like this:首先,您需要在活动中找到RecyclerView并像这样设置LinearLayoutManager

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Then you need to create the root reference of your Firestore database and a Query object like this:然后您需要创建 Firestore 数据库的根引用和一个Query对象,如下所示:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("products")
        .orderBy("productName", Query.Direction.ASCENDING);

Then you'll have to create a FirestoreRecyclerOptions object like this:然后你必须像这样创建一个FirestoreRecyclerOptions对象:

FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
        .setQuery(query, ProductModel.class)
        .build();

In your activity class, create a holder class that looks like this:在您的活动类中,创建一个如下所示的holder类:

private class ProductViewHolder extends RecyclerView.ViewHolder {
    private View view;

    ProductViewHolder(View itemView) {
        super(itemView);
        view = itemView;
    }

    void setProductName(String productName) {
        TextView textView = view.findViewById(R.id.text_view);
        textView.setText(productName);
    }
}

Then create an adapter which is declared as global:然后创建一个声明为全局的adapter

private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;

And instantiate it in your activity like this:并在您的活动中实例化它,如下所示:

adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
    @Override
    protected void onBindViewHolder(@NonNull holder productViewHolder, int position, @NonNull ProductModel productModel) {
        holder.setProductName(productModel.getProductName());
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }
};
recyclerView.setAdapter(adapter);

In the end, don't forget to override the following two methods and start listening for changes:最后别忘了重写下面两个方法,开始监听变化:

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();

    if (adapter != null) {
        adapter.stopListening();
    }
}

The result is this:结果是这样的:

在此处输入图像描述

Edit:编辑:

If you want to display a toast message when the user clicks on an item, please add the following lines of code inside the setProductName() method from the ProductViewHolder class:如果你想在用户点击一个项目时显示提示消息,请在ProductViewHolder类的setProductName()方法中添加以下代码行:

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), productName, Toast.LENGTH_SHORT).show();
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的文档及其子集合? - How to display a document with its subcollections from Firestore in a RecyclerView with Android? android - 如何在 RecyclerView 中显示来自 Cloud FireStore 的 object 数组 - android - how to display the array of object from Cloud FireStore in the RecyclerView Firestore 数据无法显示在 RecyclerView 上 - Firestore data can't display on RecyclerView 如何从循环中获取 firestore 数据并发送到 recyclerview 适配器 - How to get firestore data from a loop and send to recyclerview adaptor 显示来自 Firestore 的数据 - Display data from firestore 如何从 Firestore (Kotlin) 中的嵌套地图数组中为 RecyclerView 检索数据 - How to retrieve data from nested array of maps in Firestore (Kotlin) for RecyclerView Flutter || Firestore:如何从 firestore 中检索数据并将其显示在 DataTable 中 - Flutter || Firestore: how to retrieve data from firestore and display it in the DataTable 如何在android studio中显示firestore数据库中navigation header中的用户数据 - How to display the user data in navigation header from firestore database in android studio 如何从 firestore 检索数据以显示在多个 select 表单字段中 - How to retrieve data from firestore to display in multi select form field 如何将 integer 从 firebase firestore 显示到 Android Studio 的 TextView? - How to display integer from firebase firestore to Android Studio's TextView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM