1

App shows images in a Gridview. In the adapter of GridView, I generate # of images reqd randomly from the resources available. A new Integer[] imageIDs is created & those resource IDs are saved in this new array. I also want to store this resourceID in an ArrayList of custom object ImageSourceObject that extends ImageView. I set the imageView using imgObj.setImageResource(imageIDs[i]). Now when I retrieve this object in onTouch() or getView() methods I expect to get the same resource of the imageView set, whereas I get -1.

I think that when I am setting the ImageView of ImageSourceObject thru setImageResource(), then when I try to retrieve the getId() of ImageSourceObject I should get the id of the ImageView set.

Code :

    private void createObjectsArray() {
    int totalObjs = 15;
    Integer[] mThumbIds = {
            R.drawable.droid_1, R.drawable.droid_3,
            R.drawable.droid_2, R.drawable.ic_launcher
    };
    // Create new [] for imageIDs to store the resource ID as generated    
    imageIds = new Integer[totalObjs];
    // Create ArrayList of ImageSourceObject to set image & other proeprties 
    this.imgObjsArr = new ArrayList<ImageSourceObject>();

    Random random = new Random();
    int index;
    // Set Targets 
    for (int i=0; i < totalObjs; i++) {
        index = this.generateRandomNumber(0, (mThumbIds.length - 1), random);
        imageIds[i] = mThumbIds[index];
        ImageSourceObject iso = new ImageSourceObject(mContext);
        iso.setImageResource(imageIds[i]);
        imgObjsArr.add(iso);

        Log.d("GA", "ThumbIDS ID : " + mThumbIds[index] + " ImageIDs  : " + imageIds[i] + " ISO IMG ID : " + iso.getId());
    }
}

Log of this code :

02-19 12:21:38.780: D/GA(1613): ThumbIDS ID : 2130837557 ImageIDs  : 2130837557 ISO IMG ID : -1

You can see the IDs of mThumbIDs & imageIDs values are the same. But for iso object after setting the image also it is -1 ??

Code of ImageSourceObject is quiet simple :

public class ImageSourceObject extends ImageView {

private boolean touched;
private boolean targetObj;

public ImageSourceObject(Context context) {
    super(context);
    setTouched(false);
    setTargetObj(false); 
}

 .... Rest Getter & Setter methods

Can you help me know why is the resource ID not being set in ImageSourceObject ? Doesn't setting image thru setImageResource of ImageView save it with it's all resources ???

Any help is highly appreciated.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125

4 Answers4

1

you need to use iso.setId(int) for setting Id

Nooh
  • 1,548
  • 13
  • 21
1

The id you gave is for the Image you populate to your ImageSourceObject (the Drawable), it is not of the ImageSourceObject itself. if you want to set the id to be this number, you should do:

        iso.setImageResource(imageIds[i]);
        iso.setId(imageIds[i]);
yshahak
  • 4,996
  • 1
  • 31
  • 37
  • Pay attention that setImageDrawable(getResources().getDrawable(imageIds[i])) will be better aproach, because setImageResource block the UI Thread. – yshahak Feb 19 '15 at 08:39
  • Then you mean, wherever I have used setImageResource() I should use setImageDrawable(). That wouldn't raise any errors with my existing code, right ? – Tvd Feb 19 '15 at 09:28
  • Thanks. Can you also help me on http://stackoverflow.com/questions/28603664/android-update-image-in-gridview-on-touch-event-in-adapter - I want to update the image when user touches on it. I am not able to refresh it. – Tvd Feb 19 '15 at 10:14
1

For a programmatically created view like ImageSourceObject in your case, you need to assign it an id usign View.setId(int id).

If you are using an API level 17 or above you can use View.generateViewId() to generate a suitable value. According to documentation:

This value will not collide with ID values generated at build time by aapt for R.id.

Anyway, take into account that according to documentation:

The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

So, there can be collisions with your ids, and Android will resolve them giving you the first component found in the hierarchy for the given id.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • Thanks for this info. I have to support API's lower than 17, so I believe setId() will be best and most appropriate. Thanks. – Tvd Feb 19 '15 at 08:15
1

use setTag(tag) and getTag() method to set your object:

iso.setTag(imageIds[i]); //to set the id to object

int tag=(Integer)iso.getTag();// to get the id from object
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • 1
    Thanks. Setting tag should also work (I didn't try with it but am sure abt it), but I think when we can set it in normal & right way using setId() only, then why to go for setTag(), typecasting etc. – Tvd Feb 19 '15 at 08:14