android get position of clicked item in gridview

▼魔方 西西 提交于 2019-11-28 18:29:23

Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:

button.setTag(new Integer(position));

Then, in the onClickListener method, with the view that is passed in (the button) you can do:

Integer position = (Integer)view.getTag();

And then handle the position value from there.

EDIT: It appears the best practice would be to do:

button.setTag(Integer.valueOf(position));

rather than using the Integer constructor.

Mirza Adil
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        String a = String.valueOf(position);
        Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!