I am using gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() to show a list of images from a resource drawable folder. I want to know which image the user selected – not the position of it within the list, but the actual resource ID. I tried gridview.setOnItemSelectedListener, but that doesn't invoke a return to my program when an image is clicked.
Isn’t the View parameter in onItemClick suppose to point to the image that was clicked? If so, then why does View.getID() always return “no_id”, i.e., a “-1”.
Could somone please tell me what I am doing wrong?
How do you get the resource ID of what was selected? Thank you.
9/21/11 7am. Update based on comments provided thus far:
I want the resourceID that is automatcially generated at compile time & stored in R.java. I want to use it to get the same view (image) in another class. Using code like this:
Resources res = getResources();
Drawable v = res.getDrawable(resid); //resid is from View.getID()
I just don’t understand why the Adapter passes all of the information on the View EXCEPT original-stored-generated-R.java Resource-ID. It's my understanding that without it, you cannot get the same View usining a getDrawabale or getAnything. I'm sure that others have done this successfully. I just cannot find an example anywhere.
1. public void onCreate(Bundle savedInstanceState) {
2. super.onCreate(savedInstanceState);
3. setContentView(R.layout.picgridview);
4. GridView gridview = (GridView) findViewById(R.id.gridviewforpics);
5. gridview.setAdapter(new PicImageAdapter(this));
6. gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
7. public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
8. Intent answer = new Intent();
9. int resid = v.getId(); // ß ALWAYS RETURNING –1 ?????
10. answer.putExtra("resid",resid);
11. setResult(RESULT_OK, answer);
12. }
13. });
14. };// eof method