【问题标题】:ExpandableListView not showing up in my ActivityExpandableListView 未显示在我的活动中
【发布时间】:2017-08-01 12:00:51
【问题描述】:

我正在尝试在我的应用中实现 ExpandableListView,但它没有显示出来。

我仔细检查了:

  1. 正确的命名和引用——我所有的三个 XML 布局(对于 ExpandableListView、列表标题和列表项),

  2. 已调试以查看我的列表是否以 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)
    }

这是我想要实现的屏幕截图:

【问题讨论】:

  • 那么您究竟想要填充什么?有色物品还是其他东西?意味着您的 lsitview 在活动中是空的?
  • @AjayPandya 是的。我的标题和子项都没有显示。视图完全是空的。
  • @lidkxx 是 expandable_list RecyclerView?如果是这样,您需要指定layoutManager(很可能是LinearLayoutManager

标签: android list kotlin expandablelistview expandablelistadapter


【解决方案1】:

您已经非常接近解决问题了 :)

 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

【讨论】:

  • 非常感谢您的帮助!我认为这就是我一直在寻找的东西(不能说 100%,因为我找不到一些讨厌的库“/system/lib64/libhwuibp.so”,现在正在发送 SIG 9 错误,但这似乎是另一个问题)。我猜,一个小小的文档永远不会杀死任何人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多