android - Kotlin - 将项目添加到 ExpandableListView

标签 android kotlin

我正在尝试使用 Kotlin 在 Android Studio 中动态填充我的可扩展 ListView 。但截至目前,我无法找到任何最新的功能来执行此操作,我发现的所有功能似乎都已过时。这是我的骨架代码:

private val shows = listOf("First", "Breaking Bad", "Game of Thrones", "Bob and Martin...")

    private val expandableListView: ExpandableListView by bind(R.id.theListView)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Add items to expandable list view here
    }

提前致谢

最佳答案

对我来说效果很好。
在您定义列表数据的 Activity 中执行此操作:

private ArrayList<HeaderInfo> SectionList = new ArrayList<>();
...
//create the adapter by passing your ArrayList data
listAdapter = new ExpandableListAdapter(MyListActivity.this, SectionList);
//attach the adapter to the list
expandableListView.setAdapter(listAdapter);

这是我的可扩展列表适配器中的构造函数

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<HeaderInfo> SectionList;

public ExpandableListAdapter(Context context, ArrayList<HeaderInfo>
SectionList) {   
    this.context = context;
    this.SectionList = SectionList;
}
...

关于android - Kotlin - 将项目添加到 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44624412/

相关文章:

java - 从其他类创建私有(private)内部类的对象

kotlin - 将插件添加到现有的 Ktor 项目

java - 为什么 UML 显示 Android Adapter 接口(interface)有一个 View 和一个 ViewGroup

java - 由于动画问题,该应用程序已停止工作

android - 通过 CLI 启动 API 级别 27 的模拟器,缺少 'x86' CPU 的模拟器引擎程序

android - 在 android 中使用蓝牙进行消息和文件传输

java - 避免在 Kotlin 中生成对象的静态实例

generics - Kotlin 合约 : assert instance on reified type parameter

java - 如何在 Mockito 中 stub 可变参数以仅匹配一个参数

python - map() 作为类成员与 map() 作为独立函数?