Skip to content Skip to sidebar Skip to footer

Arraylist With Custom Arrayadapter

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.dr

Solution 1:

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

publicWeatherAdapter(Context context, int layoutResourceId, Weather[] data)

to

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

Then obviously you would instantiate your list to change from

 Weather data[] = null;

to

ArrayList<Weather> data = null;

Solution 2:

It's way change to arraylist, this code is work

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

publicclassWeatherAdapterextendsArrayAdapter<Weather>{

Context context;
int layoutResourceId;   
ArrayList<Weather> data = null;

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

staticclassWeatherHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}
}

Solution 3:

It will be the same way but instead of creating array you will create ArratList<Weather>

take look on this

publicclassWeatherAdapterextendsArrayAdapter<Weather>{

Context context;
int layoutResourceId;   
//Weather data[] = null;
ArrayList<Weather> data = null ;

publicWeatherAdapter(Context context, int layoutResourceId, ArrayList<Weather> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewrow= convertView;
WeatherHolderholder=null;

if(row == null)
{
    LayoutInflaterinflater= ((Activity)context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);

    holder = newWeatherHolder();
    holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
    holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

    row.setTag(holder);
}
else
{
    holder = (WeatherHolder)row.getTag();
}

Weatherweather= data.get(position);
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);

return row;
}

and you will send in constructor the ArrayList<Weather>

feed me back in any issue

Post a Comment for "Arraylist With Custom Arrayadapter"