简体   繁体   中英

mouse listener only one instance works

So i am setting mouse listeners to java components in the following manner:

int[] monsters = new int[12];
        monsters[0] = MonsterSelector.BL_BLUE_BUFF;
        monsters[1] = MonsterSelector.BL_RED_BUFF;
        monsters[2] = MonsterSelector.BL_WOLVES;
        monsters[3] = MonsterSelector.BL_WRAITHS;
        monsters[4] = MonsterSelector.BL_GOLEMS;
        monsters[5] = MonsterSelector.BARON;
        monsters[6] = MonsterSelector.PU_BLUE_BUFF;
        monsters[7] = MonsterSelector.PU_RED_BUFF;
        monsters[8] = MonsterSelector.PU_WOLVES;
        monsters[9] = MonsterSelector.PU_WRAITHS;
        monsters[10] = MonsterSelector.PU_GOLEMS;
        monsters[11] = MonsterSelector.DRAGON;    
for(int monster = 1; monster < MonsterSelector.LAST+1; monster++){
        final int currentMonster = monsters[monster-1];
        ImageView iv = new ImageView(images.get(currentMonster), 50, 50);
        ivs.put(currentMonster, iv);
        ivs.get(currentMonster).addMouseListener(this); 
    }

and i have the following implementation of mouselistener in my class:

    @Override
    public void mouseReleased(MouseEvent e) {

        Component src = (Component) e.getSource();

        Log.e(TAG, ""+src.getName());
    }

Now the problem is that only the first mouselistener set really gives the output. the rest just ignore. what am i doing wrong? i cant set the same mouselistener on multiple objects?

I'd guess that either MonsterSelector.LAST isn't set to the highest value it should be. Or that you're initialising MonsterSelector.BL_BLUE_BUFF , MonsterSelector.BL_RED_BUFF , etc. to all have the same int value?

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