ExpandableListView 未显示在我的 Activity 中
ExpandableListView not showing up in my Activity
我正在尝试在我的应用程序中实现 ExpandableListView,但它没有显示。
我仔细检查了:
正确的命名和 referring-to 我所有的三个 XML 布局(对于 ExpandableListView、列表 header 和列表项),
调试以查看我的列表是否在 prepareListdata()
方法中被填充(它们是)。
我认为 问题 在我的 ExpandableListAdapterClass
中,因为
class 参数 listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>
正在用
未使用的灰色波浪线。
如何正确地告诉我的应用程序它们等于我的 activity 成员变量?还是我明显遗漏了什么?
这是我的代码:
适配器
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)
}
这是我想要实现的屏幕截图:
您已经非常接近解决问题了:)
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
是 class' 属性,这意味着它与构造函数参数 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
。这些是直接在构造函数中声明的 class' 属性。您可以在这里阅读更多关于它们的信息:https://kotlinlang.org/docs/reference/classes.html
我正在尝试在我的应用程序中实现 ExpandableListView,但它没有显示。
我仔细检查了:
正确的命名和 referring-to 我所有的三个 XML 布局(对于 ExpandableListView、列表 header 和列表项),
调试以查看我的列表是否在
prepareListdata()
方法中被填充(它们是)。
我认为 问题 在我的 ExpandableListAdapterClass
中,因为
class 参数 listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>
正在用
未使用的灰色波浪线。
如何正确地告诉我的应用程序它们等于我的 activity 成员变量?还是我明显遗漏了什么?
这是我的代码:
适配器
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)
}
这是我想要实现的屏幕截图:
您已经非常接近解决问题了:)
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
是 class' 属性,这意味着它与构造函数参数 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
。这些是直接在构造函数中声明的 class' 属性。您可以在这里阅读更多关于它们的信息:https://kotlinlang.org/docs/reference/classes.html