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

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

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

答案中没有包含完整的解释,因此我添加了这种问答式,以便可以在评论中链接到它。

最佳答案

假设您有一个如下所示的 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;}
}

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

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

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

首先,您需要在您的 Activity 中找到 RecyclerView 并像这样设置 LinearLayoutManager:

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

然后您需要创建 Firestore 数据库的根引用和一个 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();

在您的 Activity 类中,创建一个如下所示的 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);
    }
}

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

private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;

然后像这样在您的 Activity 中实例化它:

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

最后不要忘记重写下面两个方法,开始监听变化:

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

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

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

结果是这样的:

enter image description here

编辑:

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

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

关于java - 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277797/

相关文章:

javascript - 错误 : 3 INVALID_ARGUMENT: Cannot convert an array value in an array value in Firestore admin

javascript - 如何将firebase返回的数据保存到变量

android - 从响应中获取 header (改造/OkHttp 客户端)

java - 在我机器的特定端口上运行程序

Java:使用新对象时两种方式的不同

java - 当满足条件时,如何让我的脚本运行另一个脚本? ( java )

android - 我如何获得最新的 Google Play 服务版本?

android - ExoPlayer:如何判断控件是否显示?

android - 与gradle文件同步时无法解析firebase-measurement-connector-impl

java - 如何使用 Spring Security + Angular 登录/认证