开发者

view.getId() returning wrong id in OnItemClickListener

开发者 https://www.devze.com 2023-04-09 07:49 出处:网络
In my app i have a Gallery with some images in it. When the user selects an image I want to somehow retrieve the id of the selected image.

In my app i have a Gallery with some images in it. When the user selects an image I want to somehow retrieve the id of the selected image.

The int that is returne开发者_Python百科d by getId() is -1. Why am I not getting the system id for the image that was selected?

gallery.setOnItemClickListener(new OnItemClickListener() {
            @SuppressWarnings("rawtypes")
            @Override
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                int imageId = v.getId();
            }
        });

Thanks!


Use OnItemSelectedListener instead of OnItemClickListener

  gallery.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int pos, long id){
                // TODO Auto-generated method stub
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

            }
        });


use this

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        int imageId = (( ImageAdapter)parent.getAdapter()).mygetItemId(position);

        }
    }); 

in adapter class add this function

public long getItemId(int position) {
        return imagearray[position];
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消