如何在 Kotlin 中按类别获取问题
How to get a question by its category in Kotlin
我有两个活动; 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()
}}
传递类别 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)
}
}
在 QuizActivity 中
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 }
}
我有两个活动; 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()
}}
传递类别 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)
}
}
在 QuizActivity 中
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 }
}