【发布时间】:2021-05-12 07:04:35
【问题描述】:
我正在编写一个应用程序来显示药物组、药物及其信息的树状视图。本质上,它是药物组的 ExpandableListView,它在子视图中显示单个药物名称,并打开一个新片段,其中包含更多点击信息。
我一直坚持使用正确的数据填充子视图。适配器似乎正确地获取了组数据,并且从日志记录和调试来看,子数据似乎也正确传递了。但是,ExpandableListView 中子视图中的文本仅对我打开的第一组是正确的,下一组显示看似随机的内容(打开顺序无关紧要)。 childViews 的数量是正确的。详细视图 (onClick) 显示正确的信息,按下后退按钮后,菜单将显示正确的信息(但是,任何新打开的组仍然显示错误的内容)。
我已经进行了至少 20 轮检查和澄清任何可疑代码,但无济于事。
澄清截图:
list view with two groups expanded
detail view, showing correct info (but not matching that shown in list view)
list view upon returning (notice contents now shown correctly)
这是 ExpandableListAdapter:
class MedicationsListAdapter(
private val context: Context,
private val groupList: List<String>,
private val itemList: List<List<String>>
) : BaseExpandableListAdapter() {
override fun getGroupCount(): Int {
return groupList.size
}
override fun getChildrenCount(groupPosition: Int): Int {
return itemList[groupPosition].size
}
override fun getGroup(groupPosition: Int): List<String> {
return itemList[groupPosition]
}
override fun getChild(groupPosition: Int, childPosition: Int): String {
return itemList[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 true
}
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?,
): View {
var groupView = convertView
if (groupView == null) {
val layoutInflater =
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
groupView = layoutInflater.inflate(R.layout.medication_list_group, null)
val groupTextView: TextView = groupView.findViewById(R.id.text_group_name)
groupTextView.text = groupList[groupPosition]
} else return groupView
return groupView
}
override fun getChildView(
groupPosition: Int,
childPosition: Int,
isLastChild: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var childView = convertView
if (childView == null) {
val layoutInflater =
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
childView = layoutInflater.inflate(R.layout.medication_list_item, null)
childView.findViewById<TextView>(R.id.text_medication_name).text = getChild(groupPosition, childPosition)
} else return childView
return childView
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
}
这是详细视图片段:
class MedicationItemFragment : Fragment() {
private lateinit var medicationName: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//get medication name from SafeArgs
arguments?.let {
medicationName = it.getString("medicationName").toString()
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_medication_item, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// get the correct medication data
val medication: Medication = MedicationsListData().getMedication(medicationName)
// populate the view with current medication's data
view.findViewById<TextView>(R.id.text_pharmacodynamics_body).text =
medication.pharmacodynamics
view.findViewById<TextView>(R.id.text_contraindications_body).text =
medication.contraindications
}
companion object {
fun newInstance(): ParametersFragment = ParametersFragment()
}
}
这是提供适配器数据的类:
class GroupListData {
fun getItemData(): List<List<String>> {
return listOf(
listOf("amoxicillin + clavulanate","penicillin","clindamycin","vancomycin"),
listOf("epinephrine","norepinephrine","dopamine"),
listOf("metoprolol","adenosine","amiodarone"),
listOf("metoclopramide")
)
}
fun getGroupData(): List<String> {
return listOf(
"antibiotics",
"vasopressors",
"antiarrhythmics",
"antiemetics"
)
}
}
如有必要,我可以详细说明或解释任何事情。非常感谢任何帮助!
【问题讨论】:
标签: android kotlin adapter expandablelistview