简体   繁体   English

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

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

I want to make a ListView from the time that I get from a TimePicker' I succeed to add the time into a variable. 我想用从TimePicker获得的时间制作一个ListView,然后成功将时间添加到变量中。 but when I'm trying to add them into the ListView I don't get anything. 但是当我试图将它们添加到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);

Clicked button 点击按钮

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

I can see the outcome on the TextView but not on the ListView. 我可以在TextView上看到结果,但在ListView上看不到。

Once you change the data of adapter you have to invoke notifyDataSetChanged() to see the effects in the list view. 更改适配器的数据后,必须调用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

And where is your data? 您的数据在哪里? From your code you are trying to add an empty Array. 您正在尝试从代码中添加一个空数组。

addArray = new ArrayList<String>();

You just declare addArray, it dosen't have any data in it. 您只需要声明addArray即可,其中没有任何数据。 And you are adding empty array into ListView. 并且您将空数组添加到ListView。

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

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