因此,在尝试使用自定义baseadapter失败后,我尝试按照教程here进行操作。这个似乎变得更顺畅但我遇到了一个问题,可能是因为我对java缺乏了解。在教程中,他们将每个项目添加为
Weather weather_data[] = new Weather[]
{
new Weather(R.drawable.weather_cloudy, "Cloudy"),
new Weather(R.drawable.weather_showers, "Showers"),
new Weather(R.drawable.weather_snow, "Snow"),
new Weather(R.drawable.weather_storm, "Storm"),
new Weather(R.drawable.weather_sunny, "Sunny")
};
但我正在经历一个循环,所以我需要添加一些不同的东西,但我只是不确定如何去做。所以,如果有人能帮助我,我将不胜感激!
我认为它会像
Weather.add(var1, var2);
但我最初会如何宣布呢?而不是
天气weather_data [] =新天气[]
使用“.add()”应该是什么?
提前谢谢你,
泰勒
EDIT1:我现在遇到了一个新问题。适配器是为数组设置的,但现在我将其更改为列表,我必须在适配器中更改哪些内容?
这是原始适配器:
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
答案 0 :(得分:1)
您可以使用ArrayList代替使用数组,而无需声明初始容量即可添加每个项目。
ArrayList<Weather> weather_data = new ArrayList<Weather>();
在你的适配器中,你需要使用weather_data.get(i)而不是weather_data [i]