当可以在屏幕上查看加载的图像时,我的 ListView 似乎工作正常。但是当我添加一个没有图像的新项目时,该项目不在视野范围内,ListView 会继续为其加载图像。我正在使用自定义阵列适配器。
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;
}