我正在尝试在我的应用中实现ExpandableListView,但它没有显示。
我仔细检查过:
正确命名和引用我的所有三种XML布局(对于ExpandableListView,列表标题和列表项),
调试以查看我的列表是否已填入prepareListdata()
方法(它们是)。
我认为问题在我的ExpandableListAdapterClass
中
类参数listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>
正在变色
粗糙的灰色线未使用。
如何正确地告诉我的应用程序那些等于我的活动成员变量?还是有一些我不知道的东西?
这是我的代码:
适配器
class ExpandableListAdapter (context: Context, listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() {
var listDataHeader = arrayListOf<String>() //list sub-headers
var listDataChild = HashMap<String, List<String>>() //list sub-points
private val inflater: LayoutInflater = LayoutInflater.from(context)
override fun getGroup(groupPosition: Int): Any {
return this.listDataHeader[groupPosition]
}
override fun isChildSelectable(p0: Int, p1: Int): Boolean {
return true
}
override fun hasStableIds(): Boolean {
return false
}
override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View? {
val headerTitle: Int = getGroup(groupPosition) as Int
if (convertView == null) {
inflater.inflate(R.layout.main_list_group_header, parent, false)
}
val listHeader = convertView?.findViewById<TextView>(R.id.list_header)
listHeader?.setTypeface(null, Typeface.BOLD)
listHeader?.setText(headerTitle)
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return this.listDataChild[this.listDataHeader[groupPosition]]!!.size
}
override fun getChild(groupPosition: Int, childPosition: Int): Any {
return this.listDataChild[this.listDataHeader[groupPosition]]!![childPosition]
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View? {
val childText: Int = getChild(groupPosition, childPosition) as Int
if (convertView == null) {
inflater.inflate(R.layout.main_list_item, parent, false)
}
val textListChild = convertView?.findViewById<TextView>(R.id.list_item)
textListChild?.setText(childText)
return convertView
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getGroupCount(): Int {
return this.listDataHeader.size
}
}
MainActivity和prepareListData()函数的其余部分
class MainListActivity : AppCompatActivity() {
lateinit var adapter : ExpandableListAdapter
val listDataHeader = arrayListOf<String>()
val listDataChild = HashMap<String, ArrayList<String>>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mainlistactivity)
prepareListData()
adapter = ExpandableListAdapter(this, listDataHeader, listDataChild)
expandable_list.setAdapter(adapter)
}
private fun prepareListData() {
listDataHeader.add("Gathering data")
listDataHeader.add("Simulate")
val gatheringDataChildList = arrayListOf<String>()
gatheringDataChildList.add("Record data: video + accelerometer")
val simulateChildList = arrayListOf<String>()
simulateChildList.add("Driving simulator")
listDataChild.put(listDataHeader[0], gatheringDataChildList)
listDataChild.put(listDataHeader[1], simulateChildList)
}
答案 0 :(得分:0)
你们非常接近解决问题:)
class ExpandableListAdapter (context: Context, listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() {
var listDataHeader = arrayListOf<String>() //list sub-headers
var listDataChild = HashMap<String, List<String>>() //list sub-points
//...
}
在第一行指定构造函数参数,但根本不使用它们。 (var listDataHeader
是类'属性,意味着它与构造函数参数listDataHeader
不同。)
你可能想要这样的东西:
class ExpandableListAdapter (context: Context, private val listDataHeader : ArrayList<String>, private val listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() {
//no declaration of another fields
//rest of adapter code here...
}
请在构造函数参数之前注意private val
。这些是在构造函数中直接声明的类属性。您可以在此处详细了解:https://kotlinlang.org/docs/reference/classes.html