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

Ale*_*amo 7 java android firebase android-recyclerview google-cloud-firestore

RecyclerView使用Android的情况下,从现有Firestore数据库显示数据的最佳方法是什么?

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

Ale*_*amo 28

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

Firestore-root
    |
    --- products (collection)
           |
           --- documentIdOne (document)
           |        |
           |        --- productName: "Milk"
           |
           --- documentIdTwo (document)
           |        |
           |        --- productName: "Soy Milk"
           |
           --- documentIdThree (document)
                    |
                    --- productName: "Bacon"
Run Code Online (Sandbox Code Playgroud)

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

public class ProductModel {
    private String productName;

    public ProductModel() {}

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

    public String getProductName() {return productName;}
}
Run Code Online (Sandbox Code Playgroud)

一个.XML包含a 的文件RecyclerView看起来像这样:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>
Run Code Online (Sandbox Code Playgroud)

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

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

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Run Code Online (Sandbox Code Playgroud)

然后,您需要创建Firestore数据库的根引用和如下Query对象:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("products")
        .orderBy("productName", Query.Direction.ASCENDING);
Run Code Online (Sandbox Code Playgroud)

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

FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
        .setQuery(query, ProductModel.class)
        .build();
Run Code Online (Sandbox Code Playgroud)

在您的活动类中,创建一个如下所示的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个adapter声明为global的:

private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;
Run Code Online (Sandbox Code Playgroud)

并在您的活动中实例化它,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

    if (adapter != null) {
        adapter.stopListening();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此输入图像描述

编辑:

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

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