我可以将图像添加到我的 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/