簡體   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