0
@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {

        TextSwitcher textSwitcherage, textSwitcherGender;
        Button nextButton, previousButton, nextButton2, previousButton2;
        String[] age = {"U-4","Kid","Teen","Adult","Old"};
        String[] gender = {"Male","Female","Other"};
        TextView textView;
        Typeface typeface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        defVars();
    }

    public void defVars()
    {
        textSwitcherGender = findViewById(R.id.textSwitcherGender);
        textSwitcherage = findViewById(R.id.textSwitcherage);
        nextButton = findViewById(R.id.nextButton1);
        nextButton2 = findViewById(R.id.nextButton2);
        previousButton = findViewById(R.id.previousButton1);
        previousButton2 = findViewById(R.id.previousButton2);
        typeface = getResources().getFont(R.font.lettersforlearners);

        nextAndPreviousButtons(nextButton,previousButton,textSwitcherage,age,0);
        nextAndPreviousButtons(nextButton2, previousButton2, textSwitcherGender, gender,0);
    }

    public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
    {
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex == data.length - 1)
                {
                    stringIndex = 0;
                    textSwitcher.setText(data[stringIndex]);
                }
                else
                {
                    textSwitcher.setText(data[++stringIndex]);
                }
            }
        });

        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex==0)
                {
                    stringIndex = data.length-1;
                    textSwitcher.setText(data[stringIndex]);
                }
                else
                {
                    textSwitcher.setText(data[--stringIndex]);
                }
            }
        });

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                textView = new TextView(MainActivity.this);
                textView.setTypeface(typeface);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(40);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                return textView;
            }
        });

        textSwitcher.setText(data[stringIndex]);

    }
}

I have an error:

"error: cannot assign a value to final variable stringIndex"

I set the stringIndex final but still got that problem. I don't know. Am i have to define Integer variables for stringIndex? Or should I delete the stringIndex final? I need to define separate stringIndex variables for each function

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
ibrahim
  • 5
  • 5
  • It is not possible to assign a new value to a final variable (also see https://stackoverflow.com/a/15655032/4295944) – buettner123 Aug 25 '20 at 13:12

2 Answers2

0

if you declare final Integer stringIndex... then you are done! that is the idea and meaning of the reserved word final... is an object you cannot re assign after...

public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, 
   final TextSwitcher textSwitcher, final String[] data, Integer stringIndex)
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You don't have to modify stringIndex

    public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
    {
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex == data.length - 1)
                {
                    textSwitcher.setText(data[0]);
                }
                else
                {
                    textSwitcher.setText(data[stringIndex+1]);
                }
            }
        });

        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex==0)
                {
                    textSwitcher.setText(data[data.length-1]);
                }
                else
                {
                    textSwitcher.setText(data[stringIndex-1]);
                }
            }
        });

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                textView = new TextView(MainActivity.this);
                textView.setTypeface(typeface);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(40);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                return textView;
            }
        });

        textSwitcher.setText(data[stringIndex]);
Bruno
  • 3,872
  • 4
  • 20
  • 37
  • sir but i still have problem at else blocks: error: cannot assign a value to final variable stringIndex textSwitcher.setText(data[++stringIndex]); – ibrahim Aug 25 '20 at 13:22