Add list to ArrayList and show on ListView

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.




2 Answers
2



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





@Mayli Plus why use same string concatenation again in the addArray.add() when you can use addArray.add(textView.getText()) or as string concatenation is not even recommended to be used with setText(), set it to a temp string and pass it to both setText() and add().
– Lalit Singh Fauzdar
Aug 12 at 7:53



addArray.add()


addArray.add(textView.getText())


setText()


setText()


add()



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard