user1978601
user1978601

Reputation: 53

incrementing/decrementing button.onclicklistener

I have an activity class that is implementing onClickListener Interface

I am not able to find out what to do next. My aim is to have a overriding view method onClick(View v) that will increment the flag from 1 to 5 different uris (say 5 urls) on click of UP button and decrement from uri-5 to uri-1 on the click of DOWN button. It'll dependent on the position of each button at any instance. Please help me with a clue.

I had tried is as below:-

Button up = (Button) findViewById(R.id.button1);
Button down = (Button) findViewById(R.id.button2);

up.setOnClickListener(this);
down.setOnClickListener(this);

public void onClick(View v) {
}

Upvotes: 1

Views: 2744

Answers (3)

iTech
iTech

Reputation: 18430

You can do something like this:

int currentURLIndex = 0;
int urlCount = 5;

public void onClick(View v) {
  if(v == up){
    currentURLIndex = Math.min(++currentURLIndex, urlCount);
  } else if if(v == down){
     currentURLIndex = Math.max(--currentURLIndex, 0);
   }
}

Upvotes: 0

Harpreet
Harpreet

Reputation: 3070

You can try this:-

static int flag = 0;  // Global variable in class

/** Code to use in onCreate()  **/
Button up = (Button) findViewById(R.id.button1);
Button down = (Button) findViewById(R.id.button2);    
up.setOnClickListener(this);
down.setOnClickListener(this);

/** Override method in class **/
public void onClick(View v)
{
  if (v == up)
  {
    if (flag < 5)
       flag++;
  }
  else if (v == down)
  {
    if (flag > 0)
       flag--;
  }
}

Upvotes: 0

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

Try this make a static variable

static int count=0;

in up

  if(count=<5)
   Log.e("Output" ,"uri-"+count++);

in down

if(count>0)
 Log.e("Output" ,"uri-"+count--);

Upvotes: 1

Related Questions