2017-06-19 208 views
5

我想动态地使用Kotlin填充Android Studio中的可展开列表视图。但截至目前,我无法找到任何最新的功能,我发现的所有功能似乎都过时了。这是我的骨架代码:Kotlin - 将项目添加到ExpandableListView

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 
    } 

在此先感谢

+1

谷歌“ExpandableListView例如”构造函数,你应该能够找到答案。 – rozina

+0

是的,我找到了setAdapter函数。似乎很难创建一个ExpandableListAdapter。我只设法让ArrayAdapter工作,这对我没有多大帮助。 – OhMad

回答

0

对我来说它工作得很好。
在你的活动中,可以定义你的ListData做到这一点:

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