1

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

I want to use an arraylist like

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

and then store things as

weather_data.add(new new Weather(R.drawable.weather_cloudy, "Cloudy"));

But how would I go about changing

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

To work with arraylist instead of just an array.

Thank you in advance,

Tyler

EDIT1: I've tried changing

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

and

Weather weather = data.get(position);

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

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

which shows

02-13 21:06:50.542: D/allList_data(6410): [com.skateconnect.AllList@4220ecc0]
2
  • so , what's the problem you face ? Commented Feb 14, 2014 at 2:06
  • I'm not too sure on how to change the public class weatheradapter to use arraylist instead of just an array. I tried changing a couple things and it does not crash but it also does not show what it is supposed to. I'm adding something else to the main post to help with what I've tried so far, Commented Feb 14, 2014 at 2:09

3 Answers 3

2

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

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

to

public WeatherAdapter(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;
Sign up to request clarification or add additional context in comments.

10 Comments

Yup done that. But when I try it the list just shows up blank, see the edit in the main question to see what the logcat give me.
I don't see where you get weather_data... You can't just print the ArrayList in your logcat or you will get the String value of the Object. Try printing the firs element
How would I print the first element? (weather_data.get(0)).toString()?
If weather_data is an ArrayList, yes
I have it intialized as ArrayList<Weather> weather_data = new ArrayList<Weather>(); and then Log.d("weather_data",(weather_data.get(0)).toString()); and it still comes up as before.
|
1

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

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

1 Comment

No error. The image just doesn't show, everything else does though :D
1

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

take look on this

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

and you will send in constructor the ArrayList<Weather>

feed me back in any issue

1 Comment

It still doesnt, show anything in the list. It shows the row in the list but none of the text or image are in the list. just an empty row

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.