my ListView seems to be working correctly when the images loaded can be viewed on the screen. but when i add a new item with no image, and the item is out of view, the ListView keeps loading an image for it. i am using a custom arrayadapter.
int resource;
private RecipeClasses mRecipeClass;
private Recipe mRecipe;
private Context context;
Uri path;
public RecipeAdapter(Context context, int _resource, List<Recipebag> items)
{
super(context, _resource, items);
resource = _resource;
mRecipeClass = new RecipeClasses(context);
this.context = context;
mRecipe = new Recipe(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout newView;
Recipebag item = getItem(position);
long id = item.getRecId();
mRecipeClass.open();
Cursor recipe = mRecipe.setCursorToRecipe(id);
String name = recipe.getString(recipe.getColumnIndexOrThrow(RecipeClasses.Recipe.RECIPE_NAME));
String image = recipe.getString(recipe.getColumnIndexOrThrow(RecipeClasses.Recipe.RECIPE_IMAGE));
mRecipeClass.close();
String newId = String.valueOf(id);
if(image== null)
{
image = " ";
}
else
{
path = Uri.parse(image);
}
if (convertView == null)
{
// Inflate a new view if this is not an update.
newView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(inflater);
li.inflate(resource, newView, true);
}
else
{
// Otherwise we’ll update the existing View
newView = (LinearLayout) convertView;
}
TextView recipe_txt = (TextView) newView.findViewById(R.id.txt_recipe_row);
ImageView img = (ImageView) newView.findViewById(R.id.img_view_recipe);
if(new File(image).exists())
{
ImageResizer img_W = new ImageResizer(context, img.getMeasuredWidth(), img.getMeasuredHeight());
img_W.loadImage(image, img);
}
else if(image!= " ")
{
ImageResizer img_W = new ImageResizer(context, img.getMeasuredWidth(), img.getMeasuredHeight());
img_W.loadImage(path, img);
img.setImageURI(path);
}
if (recipe_txt != null)
{
recipe_txt.setText(name);
}
return newView;
}
Near as I can tell, you are never clearing the content of your
ImageViewingetView(). Hence, when the row is recycled, it will already have an image. You need to handle the case where you have no image ingetView()— trysetImageDrawable(null)or something.