我正在尝试使用 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/