Android 使用 FirestorePagingAdapter 从多个文档中查询数据
Android query data using FirestorePagingAdapter from multiple documents
我有 collection 个用户和 collection 个食谱
我正在使用 FirestorePagingAdapter 来显示和分页数据,我想显示带有用户详细信息(姓名和照片)的食谱(标题和图像..)
我可以显示食谱,但无法在同一查询中获取用户详细信息(Firestore 中没有连接)
这是我的代码:
fun setUpHomeAdapter(){
var mQuery: Query = firestore.collection("recipes")
// Init Paging Configuration
val config = PagedList.Config.Builder()
.setInitialLoadSizeHint(5)
.setEnablePlaceholders(true)
.setPrefetchDistance(5)
.setPageSize(5)
.build()
// Init Adapter Configuration
val options = FirestorePagingOptions.Builder<Recipe>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, Recipe::class.java)
.build()
mAdapter = RecipesHomePagingAdapter(options, this)
homeRecyclerView.adapter = mAdapter
}
我的适配器:
class RecipesHomePagingAdapter(
options: FirestorePagingOptions<Recipe>,
val listener: RecipesHomeViewHolder.OnItemRecipeClickListener) : FirestorePagingAdapter<Recipe,
RecipesHomeViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipesHomeViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(
R.layout.fragment_recipe_home_item,
parent,
false
)
return RecipesHomeViewHolder(view, listener)
}
override fun onBindViewHolder(holder: RecipesHomeViewHolder, position: Int, recipe: Recipe) {
holder.bind(recipe)
}
我的 ViewHolder :
class RecipesHomeViewHolder(itemView: View, private val listener: OnItemRecipeClickListener) :
RecyclerView.ViewHolder(itemView), View.OnClickListener {
private var imageRecipe: ImageView = itemView.findViewById(R.id.image_recipe)
private var title: TextView = itemView.findViewById(R.id.title)
private var nameuser: TextView = itemView.findViewById(R.id.user_name)
private var calories: TextView = itemView.findViewById(R.id.calories_textview)
private var layout_image_recipe: RelativeLayout =
itemView.findViewById(R.id.layout_image_recipe)
private var id_recipe: String?=null
fun bind(recipe: Recipe) {
Glide
.with(imageRecipe.context)
.load(recipe.imageUrl)
// .diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_recipe_placeholder)
.error(R.drawable.ic_recipe_placeholder)
.into(imageRecipe)
calories.text = recipe.calories.toString() + " Calories"
title.text = recipe.title
//nameuser.text = recipe.user_uid
}
init {
layout_image_recipe.setOnClickListener(this)
}
override fun onClick(v: View?) {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
listener.onViewClick(v!!, position, id_recipe)
}
}
interface OnItemRecipeClickListener {
fun onViewClick(v: View, position: Int, id_recipe:String?)
}
}
感谢您帮助解决问题![=15=]
遗憾的是,您不能将两个查询传递给 FirestorePagingOptions
,只允许一个。我能想到的最简单的事情是将用户详细信息添加为 Recipe
对象中的属性。如果您不能这样做,还有另一种选择,您可以在其中创建两个单独的查询并将结果传递给适配器,但在这种情况下,您将无法再使用 FirestorePagingAdapter。但是,仍然可以通过将查询游标与 limit() 方法结合使用来归档分页。为此,我建议您从以下 post:
中查看我的回答
然后看看这个 video 以便更好地理解。
如果您想使用现代 Paging 3 库,请查看以下文章:
我有 collection 个用户和 collection 个食谱
我正在使用 FirestorePagingAdapter 来显示和分页数据,我想显示带有用户详细信息(姓名和照片)的食谱(标题和图像..)
我可以显示食谱,但无法在同一查询中获取用户详细信息(Firestore 中没有连接)
这是我的代码:
fun setUpHomeAdapter(){
var mQuery: Query = firestore.collection("recipes")
// Init Paging Configuration
val config = PagedList.Config.Builder()
.setInitialLoadSizeHint(5)
.setEnablePlaceholders(true)
.setPrefetchDistance(5)
.setPageSize(5)
.build()
// Init Adapter Configuration
val options = FirestorePagingOptions.Builder<Recipe>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, Recipe::class.java)
.build()
mAdapter = RecipesHomePagingAdapter(options, this)
homeRecyclerView.adapter = mAdapter
}
我的适配器:
class RecipesHomePagingAdapter(
options: FirestorePagingOptions<Recipe>,
val listener: RecipesHomeViewHolder.OnItemRecipeClickListener) : FirestorePagingAdapter<Recipe,
RecipesHomeViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipesHomeViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(
R.layout.fragment_recipe_home_item,
parent,
false
)
return RecipesHomeViewHolder(view, listener)
}
override fun onBindViewHolder(holder: RecipesHomeViewHolder, position: Int, recipe: Recipe) {
holder.bind(recipe)
}
我的 ViewHolder :
class RecipesHomeViewHolder(itemView: View, private val listener: OnItemRecipeClickListener) :
RecyclerView.ViewHolder(itemView), View.OnClickListener {
private var imageRecipe: ImageView = itemView.findViewById(R.id.image_recipe)
private var title: TextView = itemView.findViewById(R.id.title)
private var nameuser: TextView = itemView.findViewById(R.id.user_name)
private var calories: TextView = itemView.findViewById(R.id.calories_textview)
private var layout_image_recipe: RelativeLayout =
itemView.findViewById(R.id.layout_image_recipe)
private var id_recipe: String?=null
fun bind(recipe: Recipe) {
Glide
.with(imageRecipe.context)
.load(recipe.imageUrl)
// .diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_recipe_placeholder)
.error(R.drawable.ic_recipe_placeholder)
.into(imageRecipe)
calories.text = recipe.calories.toString() + " Calories"
title.text = recipe.title
//nameuser.text = recipe.user_uid
}
init {
layout_image_recipe.setOnClickListener(this)
}
override fun onClick(v: View?) {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
listener.onViewClick(v!!, position, id_recipe)
}
}
interface OnItemRecipeClickListener {
fun onViewClick(v: View, position: Int, id_recipe:String?)
}
}
感谢您帮助解决问题![=15=]
遗憾的是,您不能将两个查询传递给 FirestorePagingOptions
,只允许一个。我能想到的最简单的事情是将用户详细信息添加为 Recipe
对象中的属性。如果您不能这样做,还有另一种选择,您可以在其中创建两个单独的查询并将结果传递给适配器,但在这种情况下,您将无法再使用 FirestorePagingAdapter。但是,仍然可以通过将查询游标与 limit() 方法结合使用来归档分页。为此,我建议您从以下 post:
然后看看这个 video 以便更好地理解。
如果您想使用现代 Paging 3 库,请查看以下文章: