jackgerrits
jackgerrits

Reputation: 821

Set the text of a toast using a position in an array that is the iteration of the loop

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);
        }

Upvotes: 0

Views: 139

Answers (2)

Sino Raj
Sino Raj

Reputation: 6441

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
                }
            });
        }
}

Upvotes: 0

Rahul
Rahul

Reputation: 45070

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])..

Upvotes: 2

Related Questions