Skip to content Skip to sidebar Skip to footer

Button To Show Previous String

I am developing a quotes app that has next/previous, and copy buttons. this is the code : Button btn1; String countires[]; int i=0; /** Called when the activ

Solution 1:

create a new member for your Activity like:

int actual = 0;

Then create a 'next' button:

nextButton = (Button) findViewById(...);

nextButton.setOnClickListener(new OnClickListener()
{
    @Overridepublic void onClick(View arg0)
    {
        actual = actual < countires.length - 1 ? actual + 1 : actual;
        String  country  = countires[actual];
        btn1.setText(country);
    }
});

Same goes for the previous button:

prevButton = (Button) findViewById(...);

prevButton.setOnClickListener(new OnClickListener()
{
    @Overridepublic void onClick(View arg0)
    {
        actual = actual > 0 ? actual - 1 : actual;
        String  country  = countires[actual];
        btn1.setText(country);
    }
});

Solution 2:

That would be:

// Previf ( i > 0 ) {
    i--;
} else {
    i = countires.length - 1;
}
Stringcountry= countires[i];
btn1.setText(country);

Edit: most sense would be to change the next button as well. Because in the next method, you're now increasing i after you set the text. That's messing up the logic quite a bit.

// Nextif ( i < countires.length - 1 ) {
    i++;
} else {
    i = 0;
}
Stringcountry= countires[i];
btn1.setText(country);

Post a Comment for "Button To Show Previous String"