Android 当没有为项目设置图像时,ListView显示为视图外的项目加载图像

Android 当没有为项目设置图像时,ListView显示为视图外的项目加载图像,android,Android,当可以在屏幕上查看加载的图像时,我的ListView似乎工作正常。但是,当我添加一个没有图像的新项目,并且该项目不在视图中时,ListView会继续为其加载图像。我正在使用自定义阵列适配器 int resource; private RecipeClasses mRecipeClass; private Recipe mRecipe; private Context context; Uri path; public R

当可以在屏幕上查看加载的图像时,我的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;
    }
int资源;
私有RecipeClasseMRECIPClass;
私家菜谱;
私人语境;
Uri路径;
公共RecipeAdapter(上下文、int\u资源、列表项)
{
超级(上下文、资源、项目);
资源=_资源;
mRecipeClass=新的RecipeClasses(上下文);
this.context=上下文;
mRecipe=新配方(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
线性布局newView;
Recipebag item=getItem(位置);
long id=item.getRecId();
mRecipeClass.open();
游标配方=mRecipe.setCursorToRecipe(id);
字符串名称=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=“”;
}
其他的
{
path=Uri.parse(图像);
}
if(convertView==null)
{
//如果这不是更新,则为新视图充气。
newView=newlinearlayout(getContext());
字符串充气器=Context.LAYOUT\u充气器\u服务;
李平;
li=(LayoutInflater)getContext().getSystemService(充气机);
li.膨胀(资源、新视图、真实);
}
其他的
{
//否则,我们将更新现有视图
newView=(LinearLayout)convertView;
}
TextView recipe_txt=(TextView)newView.findViewById(R.id.txt_recipe_行);
ImageView img=(ImageView)newView.findViewById(R.id.img\u view\u配方);
if(新文件(image.exists())
{
ImageResizer img_W=新的ImageResizer(上下文,img.getMeasuredWidth(),img.getMeasuredHeight());
img_W.loadImage(图像,img);
}
else if(图像!=“”)
{
ImageResizer img_W=新的ImageResizer(上下文,img.getMeasuredWidth(),img.getMeasuredHeight());
img_W.loadImage(路径,img);
setImageURI(路径);
}
如果(配方_txt!=null)
{
配方_txt.setText(名称);
}
返回新视图;
}

就我所知,您从未在
getView()
中清除
ImageView
的内容。因此,当该行被回收时,它将已经有一个图像。您需要处理在
getView()
中没有图像的情况——尝试
setImageDrawable(null)
或其他方法。

您应该只考虑没有图像的情况,即,在更新配方图像之前,暂时显示默认图像

试试这个

if(image != null)
{ 
   icon.setImageBitmap(image); 
    return; 
} 
图标将显示在ImageView中

如果项目资源中有图像,则设置的图像将是默认图像(R.id.default) 或
将您想要的系统资源映像复制到您的项目中,并从那里调用它。

wow…它成功了,谢谢,只需显示一个默认映像即可