I think the question is simple. But as a beginner I don't understand what to do. I have read this tutorial How to make list view with multiple textviews
My question is: How can I dynamically add an entry. Adapter.add fails. And I can't google because weather_data is of type weather and this is unknown. Thank you very much for your help.
The tutorial uses a java array which is difficult to add to after it has been created:
Weather weather_data[]
The custom adapter used doesn't override the Add method either which is why it doesn't work correctly. I suggest using an ArrayList instead that holds objects of type Weather. In MainActivity:
ArrayList<Weather> weather_data = new ArrayList<Weather>();
weather_data.add(new Weather(R.drawable.weather_cloudy, "Cloudy"));
weather_data.add(new Weather(R.drawable.weather_showers, "Showers"));
weather_data.add(new Weather(R.drawable.weather_snow, "Snow"));
weather_data.add(new Weather(R.drawable.weather_storm, "Storm"));
weather_data.add(new Weather(R.drawable.weather_sunny, "Sunny"));
In WeatherAdapter:
Change the type of the class variable 'data' from an array to an ArrayList:
Weather data[] = null;
to become:
ArrayList<Weather> data = null;
Change the constructor to accept an ArrayList instead of an array:
public WeatherAdapter(Context context, int layoutResourceId, ArrayList<Weather> data)
In the getView method, you'll need to change the syntax for getting the correct array element (use the ArrayList get method):
Weather weather = data.get(position);
Then you can add items dynamically from your MainActivity. For example:
weather_data.add(new Weather(R.drawable.weather_stormy, "Stormy"));
adapter.notifyDataSetChanged();