0

so I was trying to follow the tutorial here after a failed attempt with a custom baseadapter. This one seems to be going much smoother but I have run into a problem, probably because my lack of knowledge with java. In the tutorial they add each item as

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

But I am going through a loop so I need to add things a little differently but I'm just not sure on how to go about that. So if someone could help me that would be greatly appreciated!

I'm thinking it would be something like

Weather.add(var1, var2);

but how would I go about initially declaring it? Instead of

Weather weather_data[] = new Weather[]

What should it be to use the ".add()"?

Thank you in advance,

Tyler

EDIT1: I ran into a new problem now. The adapter was set up for an array but now that I changed it to a list what would I have to change in the adapter?

Here is the original adapter:

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

1 Answer 1

1

Instead of using an array, you could instead use an ArrayList which will allow you to add each item without having to declare an initial capacity.

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

In your adapter you would then need to use weather_data.get(i) as opposed to weather_data[i]

Sign up to request clarification or add additional context in comments.

3 Comments

would I be able to do weather_data.add(R.drawable.weather_cloudy, "Cloudy");?
You'll need to create a new Weather object, so: weather_data.add(new Weather(R.drawable.weather_cloudy, "Cloudy")); :)
I ran into another problem. I added it to the main question :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.