Search code examples
javaandroidontouchlistenerontouch

Access Loop Variables within setOnTouchListener


I am creating OnTouchListeners in a loop:

for(int i = 0; i < buttons.length; i++){
        for(int j = 0; j < buttons[i].length; j++ ){
            String buttonID = "button" + i + j;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[i][j] = (Button) findViewById(resID);
            buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    if (arg1.getAction() == MotionEvent.ACTION_DOWN){
                        socket.emit("button down", "button" + i + j);
                        return false;
                    }
                    if (arg1.getAction() == MotionEvent.ACTION_UP){
                        socket.emit("button up", "button" + i + j);
                        return false;
                    }
                    return true;
                }
            });
        }
    }

How do I access the variables i and j within the setOnTouchListener function. I can't declare them final because they are the loop variables. What is the alternative way.


Solution

  • I ended up simply getting the id of the current button back within the loop:

    for(int i = 0; i < buttons.length; i++){
            for(int j = 0; j < buttons[i].length; j++ ){
                String buttonID = "button" + i + j;
                int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                buttons[i][j] = (Button) findViewById(resID);
                buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        if (arg1.getAction() == MotionEvent.ACTION_DOWN){
                            socket.emit("button down", getResources().getResourceEntryName(arg0.getId()));
                            return false;
                        }
                        if (arg1.getAction() == MotionEvent.ACTION_UP){
                            socket.emit("button up", getResources().getResourceEntryName(arg0.getId()));
                            return false;
                        }
                        return true;
                    }
                });
            }
        }