【发布时间】:2022-02-04 07:03:42
【问题描述】:
我正在尝试将数据从我的HomeFragment's RecyclerView 传递到我的CategoryServiceActivity。我正在尝试传递我选择的RecyclerView 索引,这样我就可以将正确的类别名称设置为supportActionBar 上的标题,而不是将“类别”作为我的标题。在我的PopularCategoriesAdapter 上设置setOnClickListener 时是否需要传递所选名称,还是应该将其传递到我的CategoryServiceActivity 中?谢谢。
我的“类别”标题问题
我的“类别”最终结果
CategoryServiceActivity.kt
class CategoryServiceActivity : AppCompatActivity() {
lateinit var selectedCategory: Category
val jobServices = ArrayList<JobService>()
val jobServicesDatabaseRef = FirebaseDatabase.getInstance().reference.child(REF_JOB_SERVICES)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_category_service)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setTitle("Category")
}
}
Category.kt
data class Category constructor(val category: String, val categoryImage: String)
HomeFragment.kt
lateinit var popularCategoriesAdapter: PopularCategoriesAdapter
val categories = ArrayList<Category>()
popularCategoriesAdapter = PopularCategoriesAdapter(categories)
val popularCategoriesRecyclerView = view.findViewById<RecyclerView>(R.id.popularCategoriesRecyclerView)
popularCategoriesRecyclerView.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = popularCategoriesAdapter
setHasFixedSize(true)
}
categoriesDatabaseRef.orderByChild("category").addValueEventListener(object: ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
categories.clear()
for (snap in snapshot.children) {
val category = Category(snap.child("category").getValue(String::class.java)!! ,
snap.child("categoryImage").getValue(String::class.java)!!)
categories.add(category)
}
popularCategoriesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("HomeFragment", "LoadPost:onCancelled", error.toException())
}
})
PopularCategoriesAdapter.kt
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder?.bindCategory(category[position])
holder.itemView.setOnClickListener { v ->
val context: Context = v.context
val intent = Intent(context, CategoryServiceActivity::class.java)
context.startActivity(intent)
}
}
【问题讨论】:
标签: android kotlin firebase-realtime-database android-recyclerview