谁能告诉我如何为 gridview 中的图像设置标签和获取标签。
我已经编写了以下代码,但它不起作用。以下代码是否正确?

int jj=100;
    for(int j=0;j<imageid.length;j++)//imageid is the array of drawable images
        {
        griview1.getChildAt(j).setTag(jj);
        System.out.println(g.getChildAt(j).getTag());
        }

这是我的 getView 方法
public View getView(int arg0, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub

    ImageView imv;
    if(arg1!=null)
    {
        imv=(ImageView)arg1;
    }
    else
    {
        imv=new ImageView(cont);
        imv.setLayoutParams(new GridView.LayoutParams(40,40));
        imv.setScaleType(ScaleType.CENTER_CROP);
        imv.setPadding(0,0,0,0);

        }

    }
    imv.setImageResource(imageid[arg0]);
    return imv;

最佳答案

只需在 getView 中设置您的标签

public View getView(int arg0, View arg1, ViewGroup arg2) {

    if(arg1!=null)
    {
        ImageView imv = new ImageView(cont);
        imv.setLayoutParams(new GridView.LayoutParams(40,40));
        imv.setScaleType(ScaleType.CENTER_CROP);
        imv.setPadding(0,0,0,0);

        arg1 = imv;
    }
    arg1.setTag(arg0);
    return arg1;
}

10-06 12:38