开发者

How to access the view’s resource ID of a selected image in a grid in Android?

开发者 https://www.devze.com 2023-04-06 20:09 出处:网络
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 positi

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


Inside your onItemClick() method, you coud use:

ImageAdapter i = (ImageAdapter)parent.getAdapter();

Now, you can get the ItemID with

i.getItemId(position)

So, if you type

Toast.makeText(MainActivity.this, "" + i.getItemId(position), Toast.LENGTH_SHORT).show()

You will be shown the ItemID

From here you can pass the image to other activities with an intent

For how to send images to other activities with intents, see How to pass drawable between activities

Hope it helps


I have figured out a solution.

You need to set each view a id, view.setId(R.drawable.my_pic) when you create the view.

I set mine to a pictures int value thus I can find and set this image later.

Then you can reatrieve this id and use it however you like view.getId(). Hope this helps


I checked the android source code for the View.java and from what I understood,

id of a view is by default set to 'NO_ID'

The dynamic id's that you are referring should be the id's android generate when we set id's in the xml file as android:id="@+id/layout_id". When this is encountered android generates something like this in its R file

public static final class id {
    public static final int layout_id=0x7f060000;
}

So if you need to use an id, you need to set it in the adapter.


The onItemClick() method has view within the AdapterView that was clicked as parameter v. To get the id of this View do simply

v.getId()
0

精彩评论

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

关注公众号