繁体   English   中英

将列表添加到ArrayList <String> 并显示在ListView上

[英]Add list to ArrayList<String> and show on ListView

我想用从TimePicker获得的时间制作一个ListView,然后成功将时间添加到变量中。 但是当我试图将它们添加到ListView时,我什么也没得到。

    addArray = new ArrayList<String>();
    ArrayAdapter<String> adapter = new ArrayAdapter(chart_houres_cosher.this,android.R.layout.simple_list_item_1,addArray);
    ListView listView = (ListView) findViewById(R.id.leassons);
    listView.setAdapter(adapter);

点击按钮

    textView.setText( firstHouer + " : " + firstdMinute + " - " + seccoundHouer + " : " + seccoundMinute );
    addArray.add(firstHouer + " : " + firstdMinute + " - " + seccoundHouer + " : " + seccoundMinute);

我可以在TextView上看到结果,但在ListView上看不到。

更改适配器的数据后,必须调用notifyDataSetChanged()才能在列表视图中查看效果。

textView.setText( firstHouer + " : " + firstdMinute + " - " + seccoundHouer + " : " + seccoundMinute );
addArray.add(firstHouer + " : " + firstdMinute + " - " + seccoundHouer + " : " + seccoundMinute);
addArray.notifyDataSetChanged();
//^^^^^^^^^^^^^^^^^^^
// once adapter is linked and later data source is modified , notify it
// only then adapter go though the data source 
//and update the listView/RecyclerView accordingly

您的数据在哪里? 您正在尝试从代码中添加一个空数组。

addArray = new ArrayList<String>();

您只需要声明addArray即可,其中没有任何数据。 并且您将空数组添加到ListView。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM