简体   繁体   中英

Android : Custom object can't preserve resource ID

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

您需要使用iso.setId(int)来设置ID

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]);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM