带有自定义ArrayAdapter的ArrayList

带有自定义ArrayAdapter的ArrayList

本文介绍了带有自定义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.

先谢谢您

泰勒

我试图改变

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]

推荐答案

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:29