0
所以我試圖按照教程here與自定義baseadapter一個失敗的嘗試之後。這一個似乎更順暢,但我遇到了一個問題,可能是因爲我缺乏對java的知識。在本教程中,他們添加的每個項目爲定製的ListView與ArrayAdapter
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 [] =新氣象[]
它應該是什麼用 「新增()」?
謝謝你在前進,
泰勒
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;
}
}
我能做到weather_data.add(R.drawable.weather_cloudy,「多雲「);? – TylerM
您將需要創建一個新的天氣對象,因此: 'weather_data.add(新的天氣(R.drawable.weather_cloudy,「Cloudy」));' :) – daentech
謝謝:D我認爲這工作! – TylerM