所以我试着按照教程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")
};
我想使用像
这样的arraylistArrayList<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而不仅仅是数组。
提前谢谢你,
泰勒
EDIT1: 我试过改变
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”,weather_data.toString());
显示
02-13 21:06:50.542: D/allList_data(6410): [com.skateconnect.AllList@4220ecc0]
答案 0 :(得分:2)
对于初学者,在构造函数中只需更改它以使用
中的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;
答案 1 :(得分:1)
它将采用相同的方式,但您不会创建数组,而是创建ArratList<Weather>
看看这个
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
//Weather data[] = null;
ArrayList<Weather> data = null ;
public WeatherAdapter(Context context, int layoutResourceId, ArrayList<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.get(position);
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
您将发送构造函数ArrayList<Weather>
在任何问题上反馈我
答案 2 :(得分:1)
这是对arraylist的改变,这段代码是工作
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"));
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
ArrayList<Weather> data = null;
public WeatherAdapter(Context context, int layoutResourceId, ArrayList<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.get(position);
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}