fairly99
fairly99

Reputation: 25

OnClickListener doesnt "listen"

I'm trying to listen to increasingly number of clicks, but the "OnClickListeners" method won't even listen to one of them, if I put it in a "for" loop. Here's the code:

show_seq(0);
    while (!check) {

                          // k = 0 at first 

        for (int listen = 0; listen <= k; listen++) { 
            OnClickListeners();
        }
        checkWin();
    }

and the "OnClickListeners" method:

private void OnClickListeners() {
    bird.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            getButton(1);
        }
    }); ...

any ideas?

Upvotes: 0

Views: 75

Answers (2)

Ramy Sabry
Ramy Sabry

Reputation: 176

if You try to listen to num of clicks done on one Button ..

    bird.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        sum++;System.out.printLn(sum);
    }
});

put int sum=0;

may be i don`t understand your question right :( ..

Upvotes: 1

Philio
Philio

Reputation: 4185

Setting the OnCLickListener more than once will not make a difference, you should ideally set this up outside of any loop (e.g. in onCreate).

It looks like you might be approaching this in the wrong way, what is the while loop doing? You should try and implement event listeners in such a way that they are reactive and that the event will trigger any other additional code you need to happen. If you're looking to catch multiple clicks of a view then you should easily be able to achieve this within the listener code.

Upvotes: 0

Related Questions