【问题标题】:Kotlin - Add items to ExpandableListViewKotlin - 将项目添加到 ExpandableListView
【发布时间】:2017-06-19 07:17:26
【问题描述】:

我正在尝试使用 Kotlin 在 Android Studio 中动态填充我的可扩展列表视图。但截至目前,我无法找到任何最新的功能来做到这一点,我发现的所有功能似乎都已经过时了。这是我的骨架代码:

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
    }

提前致谢

【问题讨论】:

  • 谷歌“ExpandableListView 示例”,您应该能够弄清楚。
  • 是的,我找到了 setAdapter 函数。似乎只是在努力创建 ExpandableListAdapter。我只设法让 ArrayAdapter 工作,这对我没有多大帮助。

标签: android kotlin


【解决方案1】:

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

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;
}
...

【讨论】:

    猜你喜欢
    • 2013-09-08
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多