views:

43

answers:

2

Hi guys

I am using a custom ArrayAdapter in order to populate a ListView in my page. This is the initation code of that adapter:

public IngredientListAdapter(Context context, int layout_resourceid, ArrayList<RecipeItem> items) {
    super(context, layout_resourceid);
    this.items = items ;
    ctx = context;
}

To this array adapter, I pass an array of RecipeItems (inside my ListRecipeItems ListActivity class) as such:

iAd = new IngredientListAdapter(this, R.layout.recipeitem_row, recipe.Ingredients );              
     this.setListAdapter(iAd) ;

However, I find that the ListView is not populated, despite the recipe.Ingredients ArrayList definitely being populated correctly.

A check of iAd.items.getCount() confirms that the items are there. The only way that I can get the ListView to populate is by then manually adding each item to it with .add() e.g.;

for(int i=0;i<recipe.Ingredients.size();i++)
{ 
  iAd.add(recipe.Ingredients.get(i)); 
}

My understanding was that passing a full array to the ArrayAdapter on initialization would automatically populate that ListView. However it seems that the getView method of the Adapater is never being called.

Could anyone shed any light on this behaviour? I am pretty new to android and java so I may have missed something obvious.

Many thanks Nick

+1  A: 

It looks like you are not passing your items through to the super class, so the ArrayAdapter doesn't know anything about your items collection. Try passing items as the third parameter when you invoke the super class constructor:

super(context, layout_resourceid, items);
Dan Dyer
Fantastic, that works! I knew it was something simple that I had missed.Thanks Dan :)
Nick
A: 

If I pass an ArrayList of things when I set the Adapter, it doesn't populate either. I was dealing with this just this morning. If I just pass in an empty ArrayList, then do

iAd.add(object); 

and then call

iAd.notifyDataSetChanged();

the items will show up.

Andrew
Hi Andrew.Dan's solution worked for me (see above). I wasn't passing the array through to the super constructor. This may be your problem too?
Nick
No, I am passing the array to the super constructor
Andrew