×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Image list with listener

    Hi

    Im new here so be patient!

    I'm building my first midlet and I want to have 7 pictures displayed and
    I want the app to listen to which image the user selected. Is this available
    in a form? The pictures is 50x50 px.
    Programming in J2ME

    Hope u understand me!

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Image list with listener

    Welcome to the wonderful world of mobile Java!! :-)

    It depends what you mean by "selected".

    What you cannot do: as the user navigates around the forum, different items get "focus". You cannot tell which item has focus.

    What you can do: attach a Command to an ImageItem, and set it to be the item's default command. Then, receive an event when the item is selected. "Selected" might require that a softkey be pressed, or a double-tap on a touch screen, for example.

    PHP Code:
    public class myForm extends Form implements ItemCommandListener {

        private static final 
    Command defaultCommand = new Command("Select"Command.ITEM1);

        
    // call this to add an image to the form
        
    public void addImage(Image img) {
            
    // create an ImageItem to display the Image
            
    ImageItem item = new ImageItem(nullimgImageItem.LAYOUT_DEFAULTnull);
            
    // give it a command, which will fire when the user "selects" the image
            
    item.setDefaultCommand(defaultCommand);
            
    // make sure the event gets sent to this object
            
    item.setItemCommandListener(this);
            
    // add the item to this form
            
    append(item);
        }

        
    // implement the ItemCommandListener interface
        
    public void commandAction(Command cItem item) {
            if (
    == defaultCommand && item instanceof ImageItem) {
                
    Image selectedImage = ((ImageItemitem).getImage();
                
    // do something with the image
            
    }
        }

    Hope that helps.

    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Re: Image list with listener

    That helped a lot! Thx

    One more question about this, is there anyway I can know
    which image the user selected (in number, like getSelectedIndex() does for a list)
    This because i want the user to choose a icon (from a form with all the icons in a bigger size)
    and then use the selected image for a icon in a list. I have 2 arrays with identical images but with
    different sizes on the images.

    This is what I wrote but it seems like it does not like it...

    PHP Code:
     public void commandAction(Command cItem item) {
            if (
    == defaultCommand && item instanceof ImageItem) {
                
                
    Image selectedImage = ((ImageItemitem).getImage();
                  for(
    int i=0;i<dev_pics.length;i++)
                      if(
    selectedImage == dev_pics[i])
                      
    // dev_pics is a imagearray where i've put all the images
                        
    new_dev.set(device,device2,dev_pics_small[i]); 
                        
    /* new_dev is a list with some elements
                         * dev_pics_small is a imagearray with the same pictures but 
                         * in a smaller version.
                         */                  
                    
                
    display.setCurrent(new_dev);
            }
         } 

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Image list with listener

    Hmmm... that looks like it should work.

    One thing about comparing objects with "==" is: you are not comparing objects. You are comparing references to objects.

    PHP Code:
    Image img1 Image.createImage("/face.png");
    Image img2 Image.createImage("/face.png");
    Image img3 img1;

    if (
    img1 == img2) {
        
    // never true... they are identical images, but different objects
    }

    if (
    img1 == img3) {
        
    // true, because these variables refer to the same object

    My question is: are you loading the same image more than once?

    Cheers,
    Graham.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Re: Image list with listener

    Yes that was it!

    Thx!

Similar Threads

  1. Can I adjust the size of image in my List?
    By baihaoping in forum Mobile Java General
    Replies: 1
    Last Post: 2006-04-15, 00:47
  2. How do I insert a list on a background image?
    By amreldeeb in forum Mobile Java General
    Replies: 1
    Last Post: 2006-01-10, 11:45
  3. 6680 and bluetooth service profiles
    By ceruault in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-10-08, 22:24
  4. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 09:58
  5. Background image for the list box
    By AnuPrasad in forum Symbian C++
    Replies: 0
    Last Post: 2004-09-09, 13:04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •