Wednesday, August 3, 2011

Loading image data with a title

Ok, there are still a few items I want to take care of. One of them is a couple of the artist's images aren't quite right...one's too big, and the other the definition seems off. The first one is easy enough to get rid of, but the second one, I forgot the name. I'll just search for it now by testing the app at one word per level. Ok, it's called the third something.

Whoa - the titles don't match the pictures! Man, that was a close one! There are so many things that can go wrong. How could I prevent that in the future? At the very least, make one single check. Man, that would've been embarrassing. The other thing is contact the artist, get her a copy, take a look at it. Well, let's at least fix up the problem. Man.

Is it an off by one error?

if (null == imageNameHash) {
imageNameHash = new Hashtable();

imageNameHash.put("all_was_darkness", "All was Darkeness");
imageNameHash.put("fragile_as_snow", "Fragile as Snow");
imageNameHash.put("hanami_looking_at_cherries",
"Hanomi Looking at Cherries");

etc.

// Get a list of the resource ids
int[] imageResourceIds = new int[] {

R.drawable.all_was_darkness,
R.drawable.fragile_as_snow,
R.drawable.hanami_looking_at_cherries,


int index = gen.nextInt(imageNames.length);


// Get a list of of the image names so we can pull it out with
// a random subscript
Object[] imageNames = imageNameHash.keySet().toArray();

// get the image name
String imageName = (String) imageNames[index];


// Set the image
mImage.setImageResource(imageResourceIds[index]);

// get the title
String title = imageNameHash.get(imageName);


The index is a random number. How am I matching that random number with the actual string of the title?

I'm creating an array, and assuming the keySet converted to an array is in the same order as they were placed in the hash - clearly an erroneous assumption. The solution is to first assign the names into the array, then push them into the hash table.

Actually, all I really need is the title, because the image can be found in resource id.

So add this

ArrayList imageTitleVector = new ArrayList();

imageTitleVector.add("All was Darkeness");
imageTitleVector.add("Fragile as Snow");

etc.

And get rid of the hash table and the conversion to array.

Then,

// random index
int index = gen.nextInt(imageTitleVector.size());

// get the title for the index
String title = imageTitleVector.get(index);

Ok, good. Now, the titles matching images of the paintings. Phew - luckily I caught that one. Ok, that's wrap!

No comments:

Post a Comment