如何在 Kotlin 中按类别获取问题

Posted

技术标签:

【中文标题】如何在 Kotlin 中按类别获取问题【英文标题】:How to get a question by its category in Kotlin 【发布时间】:2021-09-29 14:17:15 【问题描述】:

我有两个活动; CategoryActivity 和 QuizQuestionActivity。我希望当用户单击特定类别时,该特定类别的问题会显示在 QuizQuestionActivity 中,我不知道该怎么做,非常感谢您的帮助。 到目前为止,这是我的code

class CategoryModel(var id: Int, var name: String, var image: Int)

class CategoryActivity : AppCompatActivity(), OnCategoryItemClickListener 
private lateinit var binding: ActivityCategoryBinding
private lateinit var categoryAdapter: CategoryAdapter

override fun onCreate(savedInstanceState: Bundle?) 
    binding = ActivityCategoryBinding.inflate(layoutInflater)
    super.onCreate(savedInstanceState)
    setContentView(binding.root)

    initialRecyclerView()
    initialData()





private fun initialRecyclerView() 
    binding.categoryRecyclerview.apply 
        layoutManager = GridLayoutManager(this@CategoryActivity, 2)
        categoryAdapter = CategoryAdapter(arrayListOf(), this@CategoryActivity)
        adapter = categoryAdapter
    

private fun initialData() 
    val categoryList = ArrayList<CategoryModel>()
    categoryList.apply 
        add(CategoryModel(1, "History", R.drawable.history))
        add(CategoryModel(2, "Bible", R.drawable.bible))
        add(CategoryModel(3, "Geography", R.drawable.geography))
        add(CategoryModel(4, "Biology", R.drawable.biology))
        add(CategoryModel(5, "Arts & Culture", R.drawable.culture))
        add(CategoryModel(6, "Movies", R.drawable.movie))
        add(CategoryModel(7, "Economics", R.drawable.economy))
        add(CategoryModel(8, "Nature", R.drawable.nature))
        add(CategoryModel(9, "Music", R.drawable.music))


    

    categoryAdapter.setData(categoryList)


override fun onItemClick(item: CategoryModel, position: Int) 
    Toast.makeText(this, "$item.name Quiz Selected ", Toast.LENGTH_SHORT).show()
    Intent(this, QuizQuestionActivity::class.java).also  intent ->
        intent.putExtra("CATEGORY_NAME", item.name)
        startActivity(intent)

    
    getQuestionsByCategory()


fun getQuestionsByCategory(categoryId: Int, position: Int) 
    code for getting the questions by their category

我的问题模型

class Question(
val id: Int,
val questionText: String,
val optionOne: String,
val optionTwo: String,
val optionThree: String,
val optionFour: String,
val correctAnswer: Int,
val categoryId: Int)

我的测试题:

object Constants 
fun getQuestions(): ArrayList<Question> 
    val questionList = ArrayList<Question>()

    val que1 = Question(
        1,
        "The Dead Sea Scrolls were found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        1
    )
    questionList.add(que1)

    val que2 = Question(
        2,
        "nsdkkaffa",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        2
    )
    questionList.add(que2)

    val que3 = Question(
        3,
        "sfesfefsfe ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        3
    )
    questionList.add(que3)

    val que4 = Question(
        4,
        "The fewwwww in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        4
    )
    questionList.add(que4)

    val que5 = Question(
        5,
        "The www vdfe found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        5
    )
    questionList.add(que5)

    val que6 = Question(
        6,
        "The Dedvdrgegee found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        6
    )
    questionList.add(que6)

    val que7 = Question(
        7,
        "The Dearegergergels were found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        7
    )
    questionList.add(que7)

    val que8 = Question(
        8,
        "The Dead rgergrgeggegfound in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        8
    )
    questionList.add(que8)

    val que9 = Question(
        9,
        "The Dead Seareewwlls were found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        9
    )
    questionList.add(que9)

    val que10 = Question(
        10,
        "The Deadrrgrgrggs were found in 11 caves near the ruins of which place?",
        "Nahal Kidron",
        "Tel Megiddo",
        "Qumrān",
        "Wadi Qelt",
        3,
        1
    )
    questionList.add(que10)
    return questionList

这是我的 CategorAdapter 代码:

class CategoryAdapter(
private var categoryList: ArrayList<CategoryModel>,
var clickListener: OnCategoryItemClickListener):RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder>() 


/**
 * Using viewBinding
 */
class CategoryViewHolder(private val categoryBinding: CategoryBinding) :
    RecyclerView.ViewHolder(categoryBinding.root) 

    fun initialize(item: CategoryModel, action: OnCategoryItemClickListener) 
        categoryBinding.categoryName.text = item.name
        categoryBinding.categoryImage.setImageResource(item.image)

        itemView.setOnClickListener 
            action.onItemClick(item, adapterPosition)
        
    


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder 
    return CategoryViewHolder(
        CategoryBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )


override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) 
    holder.initialize(categoryList[position], clickListener)



override fun getItemCount(): Int 
    return categoryList.size


fun setData(categoryList: ArrayList<CategoryModel>) 
    this.categoryList = categoryList
    notifyDataSetChanged()

【问题讨论】:

而不是intent.putExtra("CATEGORY_NAME", item.name)传递类别ID,在QuestionActivity中接收它并通过getQuestions().find it.categoryId == passedId 找到问题 请您给我看一个示例代码,说明您的意思。请!!!! 【参考方案1】:

传递类别 ID 而不是类别名称。

override fun onItemClick(item: CategoryModel, position: Int) 
    Toast.makeText(this, "$item.name Quiz Selected ", Toast.LENGTH_SHORT).show()
    Intent(this, QuizQuestionActivity::class.java).also  intent ->
        intent.putExtra("CATEGORY_ID", item.id)
        startActivity(intent)
    

在测验活动中

class QuizQuestionActivity: AppCompatActivity() 

override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(binding.root)

    val passedId = intent.extras?.getInt("CATEGORY_ID")
    val question = getQuestions().find  it.categoryId == passedId 

【讨论】:

以上是关于如何在 Kotlin 中按类别获取问题的主要内容,如果未能解决你的问题,请参考以下文章

我如何更好地在 woocommerce rest api 中按类别对产品进行分组?

如何在 wordpress 中按简码列出类别?

如何在 Pandas 中按子类别分组? [复制]

如何在Postgres中按年月按最大(日期)组获取行?

如何在 Django 中按类别筛选产品?

如何在laravel中按类别过滤帖子?