【问题标题】:Passing Data from Fragment's RecycleView to Activity将数据从 Fragment RecyclerView 传递给 Activity
【发布时间】: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)
        }
} 

【问题讨论】:

  • 我认为 videorepo 可能会有所帮助。

标签: android kotlin firebase-realtime-database android-recyclerview


【解决方案1】:

您可以为片段添加 bundle.putString() 或为活动添加 intent.putString()。对于你的情况,也许你可以喜欢这个。

在你的片段中

val intent = Intent(context, CategoryServiceActivity::class.java)
intent.putString("key", value)
context.startActivity(intent)

在你的活动中

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")
    
    val value = intent.getStringExtra("key")
}

希望能解决你的问题

【讨论】:

  • 我能得到澄清吗?我从哪里获得“键”和“值”?
  • 对于“key”来说,它只是识别您传递的数据的关键,因为名称由您决定,并确保关键是识别传递的数据。例如,您想要传递名称,因此您可以使用 intent.putString("name", "Zachary Smouse") 的键。对于值,它意味着您要传递的数据是什么
  • 我可以通过itemView[position],因为我需要通过所选类别吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
  • 2021-05-11
  • 2021-05-12
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多