[英]How to display data from Firestore in a RecyclerView with Android?
What is the best way to display data from an existing Firestore database in a RecyclerView
using Android?使用 Android 在
RecyclerView
中显示来自现有 Firestore 数据库的数据的最佳方式是什么?
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 数据库结构:
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:还有一个包含
RecyclerView
的.XML
文件,它也如下所示:
<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
并像这样设置LinearLayoutManager
:
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:然后您需要创建 Firestore 数据库的根引用和一个
Query
对象,如下所示:
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
对象:
FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
.setQuery(query, ProductModel.class)
.build();
In your activity class, create a holder
class that looks like this:在您的活动类中,创建一个如下所示的
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);
}
}
Then create an adapter
which is declared as global:然后创建一个声明为全局的
adapter
:
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:如果你想在用户点击一个项目时显示提示消息,请在
ProductViewHolder
类的setProductName()
方法中添加以下代码行:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), productName, Toast.LENGTH_SHORT).show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.