带有自定义 ArrayAdapter 的 ArrayList [英] ArrayList with Custom ArrayAdapter

查看:45
本文介绍了带有自定义 ArrayAdapter 的 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图按照教程这里但不是使用数组来存储数据,就像他们在这里做的一样:

So I am trying to follow the tutorial here But instead of using an array to store the data like they did 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 而不仅仅是数组.

To work with arraylist instead of just an array.

先谢谢你,

泰勒

编辑 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 中查看它时:

and then when I try to view it in logcat like so:

Log.d("weather_data",weather_data.toString());

Log.d("weather_data",weather_data.toString());

显示

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

推荐答案

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

For starters, in your constructor just change it to use the ArrayList from

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

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

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

Then obviously you would instantiate your list to change from

 Weather data[] = null;

ArrayList<Weather> data = null;

这篇关于带有自定义 ArrayAdapter 的 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆