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

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

问题描述

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

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

这不作为答案中的完整解释,所以我添加了这个Q& A风格,因此它可以在评论中链接。

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.

推荐答案

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

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

以及 .XML 包含 RecyclerView 的文件,如下所示:

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.

首先,您需要在活动中找到 RecyclerView 并设置 LinearLayoutManager ,如下所示:

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

然后,您需要创建Firestore数据库的根引用和查询这样的对象:

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

然后你必须创建一个 FirestoreRecyclerOptions 像这样的对象:

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

然后创建适配器声明为全局:

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

最后,不要忘记覆盖以下两种方法并开始侦听更改: / p>

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

结果如下:

修改:

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

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天全站免登陆