Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view.getId() returning wrong id in OnItemClickListener

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 returned 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!

like image 401
plast Avatar asked Sep 30 '11 12:09

plast


1 Answers

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

            }
        });
like image 136
Venky Avatar answered Sep 20 '22 21:09

Venky