gpt4 book ai didi

android - 带有自定义 ArrayAdapter 的 ArrayList

转载 作者:行者123 更新时间:2023-11-29 16:03:28 25 4
gpt4 key购买 nike

所以我正在尝试按照教程 here但是他们并没有像这里那样使用数组来存储数据:

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")
};

我想使用像这样的数组列表

ArrayList<Weather> weather_data = new ArrayList<Weather>();

然后存储为

weather_data.add(new new Weather(R.drawable.weather_cloudy, "Cloudy"));

但是我该如何去改变

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;
}
}

使用 arraylist 而不仅仅是数组。

提前谢谢你,

泰勒

编辑1:我试过改变

ArrayList<AllList> data = null;
public WeatherAdapter(Context context, int layoutResourceId, ArrayList<Weather> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

Weather weather = data.get(position);

然后当我尝试像这样在 logcat 中查看它时:

Log.d("天气数据",weather_data.toString());

显示

02-13 21:06:50.542: D/allList_data(6410): [com.skateconnect.AllList@4220ecc0]

最佳答案

对于初学者来说,在您的构造函数中只需将其更改为使用来自

ArrayList
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data)

public WeatherAdapter(Context context, int layoutResourceId, ArrayList<Weather> data)

然后很明显你会实例化你的列表来改变

 Weather data[] = null;

ArrayList<Weather> data = null;

关于android - 带有自定义 ArrayAdapter 的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com