Friday, March 25, 2011

Guide to the Lichens of The Evergreen State College - Images

After fumbling around for a few hours I realized my knowledge of basic Java programming is too limited.  It seemed like an easy task: when a user clicks on a particular lichen in the list, an image associated with that lichen will be shown in the details view.  My idea was to take the string retrieved from the database for genus and compare it to a string constant set to different genera.  Depending on which 'If' statement evaluated to TRUE, the corresponding image would be displayed.  The logic was as follows:

IF (DATBASE VALUE = SPECIES X);
     DISPLAY IMAGE OF SPECIES X;

IF (DATABASE VALUE = SPECIES Y);
     DISPLAY IMAGE OF SPECIES Y;


I was successfully passing the correct values as a bundle and converting them to strings (shown below), so it seemed like it should be an easy task to compare two strings.


Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String genus = extras.getString(DatabaseHelper.KEY_GENUS);


The problem was figuring out how to compare these values.  Looking at other code, it seemed I should be able to use something like,


if (genus == "Cladonia") {
                mLichenGallery.setAdapter(new ImageAdapterCladonia(this));


 Hours later I came to the realization that you can't really use the '==' operator to properly compare two strings.

Instead you use the String1.Equals(String2) method call.  It seemed so easy, too good to be true, but it worked perfectly.  So now the code looks like,


    if (genus.equalsIgnoreCase(C)){
            mLichenGallery.setAdapter(new ImageAdapterCladonia(this));

Where 'C' is a string constant defined at the top of the class as 'Cladonia'.  I used the 'equalsIgnoreCase' just in case there is an error in data entry later on and the strings don't match case exactly. The images seen below are loaded depending on the user selection.





The images are just for testing and don't necessarily match the species.  Each image is part of a GalleryView that is a horizontally scrolling image view, so multiple images can be displayed and scrolled through by the user.


  

No comments:

Post a Comment