android - ListView 为项目显示不正确的图像

标签 android

我可以将图像添加到我的 ListView 并且它们可以显示,但是如果列表中的项目超过 8 个,ListView 会显示该项目的错误图像.这是我的 newView() 方法:

public View newView(Context context, Cursor cursor, ViewGroup parent){
    final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, parent, false);
try{
        int count = cursor.getCount();
        String image = cursor.getString(cursor.getColumnIndex(RecipeClasses.Recipe.RECIPE_IMAGE));
        String recipe_name = cursor.getString(cursor.getColumnIndex(RecipeClasses.Recipe.RECIPE_NAME));

        Uri path = Uri.parse(image);
        TextView recipe_txt = (TextView) view.findViewById(R.id.txt_recipe_row);
        ImageView img = (ImageView) view.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 {
            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(recipe_name);
        }
    } catch (Exception e){
        // TODO: handle exception
    }

    return view;
}

最佳答案

我假设您的 newView 类似于 getView 方法。您确定要为第 9 项获取的图像可用吗?

尝试将 Cursor 处理成 ArrayList,这样会更容易按顺序管理以确定确切的问题。

还要检查新添加的项目图像的路径格式,因为在尝试将它们分配到指定路径时它们可能不正确。

关于android - ListView 为项目显示不正确的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843614/

相关文章:

使用卷积的 Android 中值滤波器

android - 无法设置PositiveButton

java - 异步任务出错

android - 为密度和屏幕尺寸创建可绘制对象?

java - FragmentDirections 生成成功但无法解析符号

java - 基于 Android 模拟器中特定定位器的最佳向上/向下滚动方法是什么?

java - 测试应用程序时出现 NullPointerException

java - 方法中arraylist的arraylist

android - 如何为平板电脑和手机创建应用程序

android - 如何强制通知以多行显示文本?