在gridView中询问getView方法

时间:2014-03-01 14:44:38

标签: java android gridview

我想为每个gridview元素加载每个图像,但它只加载gridview的所有元素的最后一个图像。我该如何解决?我的示例图像就像a50.jpg

这是我的代码:

    for (int i = 0; i < items.size(); i++) {
    String s = Integer.valueOf(items.get(position).id_of_field).toString();
        int resourceId = context.getResources().getIdentifier("a" + s + i,
                "drawable", context.getPackageName());
        view.ivTowar.setImageResource(resourceId);
    }

2 个答案:

答案 0 :(得分:0)

getView方法中,您要为特定元素创建View,而不是为所有元素创建for。因此,正确的代码不应包含for循环。

编辑正确的代码应该是这样的,没有String s = items.get(position).id_of_field; int resourceId = context.getResources().getIdentifier("a" + s + position, "drawable", context.getPackageName()); view.ivTowar.setImageResource(resourceId); 循环(并且您尝试将字符串解析为整数然后再次获取字符串):

{{1}}

答案 1 :(得分:0)

position已经提供正在查看的单元格位置。删除你的循环。有关自定义gridview的代码和详细信息,请按照this

进行操作

或谷歌的好教程