简体   繁体   中英

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. but when I'm trying to add them into the ListView I don't get anything.

    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.

Once you change the data of adapter you have to invoke notifyDataSetChanged() to see the effects in the list view.

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. And you are adding empty array into ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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