我无法解决出现的问题,因为它位于片段中,而不在MainActivity中。我已通过多种方式重写此代码,但无法使其工作。FAQFragment中listViewAdapter中的“this”显示错误,如果我仅在MainActivity中编写此错误,则不会显示此错误。我正在尝试创建FAQ片段,最好是有一个孙子,能回答主题(父母)中的问题(孩子)。我也还没有想出如何做到这一点。请帮助或指导我找到一个更好的方法。
常见问题解答片段:
package com.app
import android.os.Bundle
import android.view.*
import android.content.Context
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_f_a_q.*
class FAQFragment : Fragment() {
private lateinit var listViewAdapter: FAQAdapter
private lateinit var questionList : List<String>
private lateinit var answerList : HashMap<String, List<String>>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
showList()
listViewAdapter = FAQAdapter(this,questionList, answerList)
FAQList.setAdapter(listViewAdapter)
return inflater.inflate(R.layout.fragment_f_a_q, container, false)
}
private fun showList() {
questionList = ArrayList()
answerList = HashMap()
(questionList as ArrayList<String>).add("Question 1")
(questionList as ArrayList<String>).add("Question 2")
(questionList as ArrayList<String>).add("Question 3")
(questionList as ArrayList<String>).add("Question 4")
(questionList as ArrayList<String>).add("Question 5")
val answer1: MutableList<String> = ArrayList()
answer1.add("Answer 1")
answer1.add("Answer 2")
answer1.add("Answer 3")
val answer2: MutableList<String> = ArrayList()
answer2.add("Answer 1")
answer2.add("Answer 2")
answer2.add("Answer 3")
val answer3: MutableList<String> = ArrayList()
answer3.add("Answer 1")
answer3.add("Answer 2")
answer3.add("Answer 3")
val answer4: MutableList<String> = ArrayList()
answer4.add("Answer 1")
answer4.add("Answer 2")
answer4.add("Answer 3")
val answer5: MutableList<String> = ArrayList()
answer5.add("Answer 1")
answer5.add("Answer 2")
answer5.add("Answer 3")
answerList[questionList[0]] = answer1
answerList[questionList[1]] = answer2
answerList[questionList[2]] = answer3
answerList[questionList[3]] = answer4
answerList[questionList[4]] = answer5
}
}
常见问题解答适配器:
package com.app
import android.content.Context
import android.view.*
import android.widget.*
class FAQAdapter internal constructor(private val context: Context, private val questionList: List<String>, private val answerList: HashMap<String, List<String>>): BaseExpandableListAdapter() {
override fun getGroupCount(): Int {
return questionList.size
}
override fun getChildrenCount(groupPosition: Int): Int {
return this.answerList[this.questionList[groupPosition]]!!.size
}
override fun getGroup(groupPosition: Int): Any {
return questionList[groupPosition]
}
override fun getChild(groupPosition: Int, childPosition: Int): Any {
return this.answerList[this.questionList[groupPosition]]!![childPosition]
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun hasStableIds(): Boolean {
return false
}
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var convertView = convertView
val questionTitle = getGroup(groupPosition) as String
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.faq_questions, null)
}
val questionTv = convertView!!.findViewById<TextView>(R.id.question_list_tv)
questionTv.setText(questionTitle)
return convertView
}
override fun getChildView(
groupPosition: Int,
childPosition: Int,
isLastChild: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var convertView = convertView
val answerTitle = getChild(groupPosition, childPosition) as String
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.faq_answers, null)
}
val answerTv = convertView!!.findViewById<TextView>(R.id.question_list_tv)
answerTv.setText(answerTitle)
return convertView
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
}
1条答案
按热度按时间yyyllmsg1#
使用
requireContext()
代替this
。