private int info = 1;
public void nextStep(View view)
{
TextView textInfo = (TextView) findViewById(R.id.textInfo);
textInfo.setText(R.string.info1);
info++;
}
When one button is clicked, method nextStep is called. And every time a button clicked, I want to show different info, first time it’s info1 string, next time it’s info2 string and etc. from strings.xml. I would like to do something like that:
private int info = 1;
public void nextStep(View view)
{
TextView textInfo = (TextView) findViewById(R.id.textInfo);
textInfo.setText(R.string.info + info);
info++;
}
Of course, it’s not possible. What should I do? I really don’t want to write a big if/else or switch statement. Thanks.
Actually, it is. You can use
getIdentifierto do this:getIdentifer()is a way to fetch resource IDs that are stored inRif you don’t know the exact name. While it isn’t the most efficient method in the world, it suffices in situations where referencingR(such as your situation) is not possible.The method returns the same ID that
Rwould; that is,getIdentifier("info1", "id", ...);is the same asR.id.info1, sinceRis just a compiled version of it. This method also works in the event that you are unsure if an ID exists (such as from an external library) but need to reference it anyway.