我是 Android 开发新手。我开发了一个应用程序,我必须在我的活动中找到来自 firestore 的生产者列表。
我有三个专业,对于每个专业,我必须在片段中显示列表。所以我的主要活动包含三个片段(viewpager),每个片段我都显示来自 firestore 的生产者列表(我按专业创建了一个集合),所以我必须从每个集合中为每个片段查找数据。我创建了一个回收适配器,我必须创建三个吗?
此外,我必须创建三个查询,其中有可能创建一个类然后用于每个片段。
我已经屏蔽了几天了。你能帮我走上正轨吗?
太感谢了。
这是我的RecyclerAdapter
课:
class RecyclerAdapter (val productList: MutableList<Product>, val context: Context) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_productcard_item, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return productList.size
}
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
val product = productList[position]
holder.speciality.text = product.speciality
holder.name.text= product.name
holder.codePostale.text = product.codePostale
holder.productCity.text = product.productCity
holder.adresse.text = product.adresse
holder.productEmail.text = product.productEmail
holder.phone.text = product.phone
holder.post.text = product.post
}
inner class ViewHolder internal constructor(view: View) : RecyclerView.ViewHolder(view){
internal var speciality : TextView = view.findViewById (R.id.speciality)
internal var name : TextView = view.findViewById(R.id.product)
internal var codePostale : TextView = view.findViewById(R.id.productCode)
internal var productCity : TextView = view.findViewById(R.id.productVille)
internal var adresse : TextView = view.findViewById(R.id.adress)
internal var productEmail : TextView = view.findViewById(R.id.product_mail)
internal var phone : TextView = view.findViewById(R.id.product_tel)
internal var post : TextView = view.findViewById(R.id.post)
}
}
data class Product( val speciality:String,
val name:String,
val codePostale:String,
val productCity:String,
val adresse:String,
val productEmail:String,
val phone:String,
val post:String
)
{
constructor(): this ("","","","","",
"","","")
fun toMap(): Map<String, Any> {
val result = HashMap<String, Any>()
result.put("speciality", speciality)
result.put("name", name)
result.put("codePostale", codePostale)
result.put("productCity", productCity)
result.put("adresse", adresse)
result.put("productEmail", productEmail)
result.put("phone", phone)
result.put("post", post)
return result
}
}
一个片段的例子
class AgriculteurFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_agriculteur, container, false)
}
}