如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? [英] How to display data from Firestore in a RecyclerView with Android?

查看:16
本文介绍了如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Android 在 RecyclerView 中显示现有 Firestore 数据库中数据的最佳方式是什么?

答案中没有包含完整的解释,所以我添加了这个问答风格,以便可以在评论中链接到.

解决方案

假设您有一个如下所示的 Firestore 数据库结构:

Firestore-root|--- 产品(集合)|--- documentIdOne(文档)|||--- 产品名称:牛奶"|--- documentIdTwo(文档)|||--- 产品名称:豆浆"|--- documentIdThree(文档)|--- 产品名称:培根"

一个看起来也像这样的模型类:

public class ProductModel {私人字符串产品名称;公共产品模型(){}公共产品模型(字符串产品名称){this.productName = productName;}public String getProductName() {return productName;}}

还有一个 .XML 文件,其中包含一个 RecyclerView 也如下所示:

如果您想在用户单击某个项目时显示 Toast 消息,请在 ProductViewHolder 类的 setProductName() 方法中添加以下代码行:

textView.setOnClickListener(new View.OnClickListener() {@覆盖公共无效onClick(查看视图){Toast.makeText(getApplicationContext(), productName, Toast.LENGTH_SHORT).show();}});

What is the best way to display data from an existing Firestore database in a RecyclerView using Android?

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

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

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<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
        .setQuery(query, ProductModel.class)
        .build();

In your activity class, create a holder class that looks like this:

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:

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:

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

这篇关于如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆