I’m using the logic below to iterate through an array names[] and show a different result on button click.
i = intent.getIntExtra(MainActivity.EXTRA_INDEX, 0);
j = i;
while (i==j)
{
j=j+1;
final TextView nameView = (TextView) findViewById(R.id.vNames);
String vName = names[i];
nameView.setText(vName);
Button nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
i=i+1;
return;
};
});
}
My problem is getting out of the onClick method with the new values. The code is getting stuck inside the onClick. (I’ve checked what’s happening using System.out.println() in various places in the code.)
I’ve used the above
public void onClick(View v) {
i=i+1;
return;
};
method, which runs without error but doesn’t give me the result. I’ve also tried something like
public int onClick(View v) {
i=i+1;
return i; //or return int i;
};
but this gives me the error “The return type is incompatible with View.OnClickListener.onClick(View)”.
Any ideas on how to deal with this or alternative methods would be appreciated.
So, what you want is to increase the value of “i” every time you press the button, then change the text on nameview? Try this:
However you will eventually run out of names. If you want to repeat the values from the start, try this: