2017-06-19 95 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; 
} 
...