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

时间:2018-03-14 12:28:07

标签: java android firebase android-recyclerview google-cloud-firestore

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

这不作为答案中的完整说明,所以我已经添加了此Q& A风格,因此可以将其链接到评论中。

1 个答案:

答案 0 :(得分:17)

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

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

看起来像这样的模型类:

public class ProductModel {
    private String productName;

    public ProductModel() {}

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

    public String getProductName() {return productName;}
}

包含.XML的{​​{1}}文件,如下所示:

RecyclerView

要显示所有产品名称,请按照以下步骤操作。

首先,您需要在活动中找到<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler_view"/> 并设置RecyclerView,如下所示:

LinearLayoutManager

然后,您需要创建Firestore数据库的根引用和RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); 对象,如下所示:

Query

然后你必须像这样创建一个FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); Query query = rootRef.collection("products") .orderBy("productName", Query.Direction.ASCENDING); 对象:

FirestoreRecyclerOptions

在您的活动类中,创建一个如下所示的FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>() .setQuery(query, ProductModel.class) .build(); 类:

holder

然后创建一个声明为global的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); } }

adapter

并在你的活动中实例化它:

private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;

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

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

结果如下:

enter image description here

修改

如果您想在用户点击某个项目时显示Toast消息,请在@Override protected void onStart() { super.onStart(); adapter.startListening(); } @Override protected void onStop() { super.onStop(); if (adapter != null) { adapter.stopListening(); } } 类的setProductName()方法中添加以下代码行:

ProductViewHolder