0

This code programatically generates a layout which consists of views and TextViews in a way that is defined in an array. However, I am trying to make each TextView clickable which displays a toast of a bigger explanation of that TextView . Since the toast needs to be specific to the particular TextView , I am trying to set the TextView using another array of explanations at position i. However, when I try to do this I get the error "Cannot refer to a non final variable inside an inner class defined in a different method."

So I am wondering, how do I get around this? i can't be final because it changes on each loop iteration?

for ( int i = 0; i < formulae.length; i++){
            if (formulae[i].equals("horizontalrule")){

                View rule = new View(FormulaeActivity.this);
                rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
                rule.setBackgroundColor(0xFF000000);

                View spacerbig = new View(FormulaeActivity.this);
                spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
                spacerbig.setBackgroundResource(android.R.color.transparent);

                container.addView(rule);
                container.addView(spacerbig);
            }

        else{
            View spacersmall1 = new View(FormulaeActivity.this);
            spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
            spacersmall1.setBackgroundResource(android.R.color.transparent);

            tx[i] = new TextView(FormulaeActivity.this);
            tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
            tx[i].setText(Html.fromHtml(formulae[i]));
            tx[i].setTextSize(20);
            tx[i].setPadding(10, 0, 0, 0);




            tx[1].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),   Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line


                }
            });


            container.addView(tx[i]);   
            container.addView(spacersmall1);
        }
flup
  • 26,937
  • 7
  • 52
  • 74
jackgerrits
  • 791
  • 2
  • 8
  • 20

2 Answers2

2

Why not have a final variable which will hold the value of i on each iteration.

for ( int i = 0; i < formulae.length; i++){
    final int pos = i;

And use it like this on the onClick()

Toast.makeText(getApplicationContext(),   Html.fromHtml(explanations[pos])..
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Ah, of course! Is that because the variable is private to that iteration? It compiles now! Thank you! Now I just have to iron out an exception... – jackgerrits Mar 28 '13 at 00:17
0

Try this,

for ( int i = 0; i < formulae.length; i++){
            if (formulae[i].equals("horizontalrule")){

                View rule = new View(FormulaeActivity.this);
                rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
                rule.setBackgroundColor(0xFF000000);

                View spacerbig = new View(FormulaeActivity.this);
                spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
                spacerbig.setBackgroundResource(android.R.color.transparent);

                container.addView(rule);
                container.addView(spacerbig);
            }

        else{
            View spacersmall1 = new View(FormulaeActivity.this);
            spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
            spacersmall1.setBackgroundResource(android.R.color.transparent);

            tx[i] = new TextView(FormulaeActivity.this);
            tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
            tx[i].setText(Html.fromHtml(formulae[i]));
            tx[i].setTextSize(20);
            tx[i].setPadding(10, 0, 0, 0);
            container.addView(tx[i]);   
            container.addView(spacersmall1);
        }
}


for(int i=0;i<tx.length;i++)
{
if(tx[i]!=null)
{
       tx[i].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line
                }
            });
        }
}
Sino Raj
  • 6,431
  • 2
  • 22
  • 23