1

当可以在屏幕上查看加载的图像时,我的 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;
    }
4

2 回答 2

0

据我所知,您永远不会清除ImageViewin的内容getView()。因此,当该行被回收时,它已经有一个图像。您需要处理没有图像的情况getView()- 尝试setImageDrawable(null)或其他东西。

于 2012-10-13T12:45:18.227 回答
0

您应该只满足无图像的情况,即暂时显示默认图像,直到您想要更新配方图像。

试试这个

if(image != null)
{ 
   icon.setImageBitmap(image); 
    return; 
} 

图标将是 ImageView

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

于 2012-10-13T13:10:14.593 回答