Android 将条目添加到文本视图
我认为问题很简单。但是作为一个初学者,我不知道该怎么做。 我读过这个教程 我的问题是:如何动态添加条目。 Adapter.add失败。 我不能谷歌,因为天气数据属于天气类型,这是未知的。Android 将条目添加到文本视图,android,add,element,custom-adapter,Android,Add,Element,Custom Adapter,我认为问题很简单。但是作为一个初学者,我不知道该怎么做。 我读过这个教程 我的问题是:如何动态添加条目。 Adapter.add失败。 我不能谷歌,因为天气数据属于天气类型,这是未知的。 非常感谢你的帮助 将新数据添加到ArrayList中,并调用函数adapter.notifyDataSetChanged() 这将刷新您的listview。并且将添加新数据。本教程使用了一个java数组,创建后很难添加到该数组中: Weather weather_data[] 所使用的自定义适配器也不会覆盖A
非常感谢你的帮助 将新数据添加到ArrayList中,并调用函数
adapter.notifyDataSetChanged()代码>
这将刷新您的listview。并且将添加新数据。本教程使用了一个java数组,创建后很难添加到该数组中:
Weather weather_data[]
所使用的自定义适配器也不会覆盖Add方法,这就是它无法正常工作的原因。我建议使用ArrayList来保存Weather类型的对象。在主要活动中:
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"));
成为:
ArrayList<Weather> data = null;
然后,您可以从MainActivity动态添加项目。例如:
weather_data.add(new Weather(R.drawable.weather_stormy, "Stormy"));
adapter.notifyDataSetChanged();
发布代码n错误日志不要直接在适配器中操作模型对象,修改活动/片段中的数据集合并调用adapter.notifyDataSetChanged谢谢。这很有效。我将在教程的评论部分添加一个链接。
public WeatherAdapter(Context context, int layoutResourceId, ArrayList<Weather> data)
Weather weather = data.get(position);
weather_data.add(new Weather(R.drawable.weather_stormy, "Stormy"));
adapter.notifyDataSetChanged();