• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Use variable in listener

Ragoune

Lurker
Hey,

I have a for loop with a listener that is created in it. In the listener, I want to use the current value of the "i" (the variable that is used for the loop).

I have this small code:
Code:
for (int i = 0; i < 3; i ++)
        	{
        	...
        	
        	description = (TextView) findViewById(descriptions[i]);
        	description.setText(feeds[i].description);
        	OnClickListener MyListener = new OnClickListener()
	    		{
				@Override
				public void onClick(View v)
					{
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(feeds[i].url));
					startActivity(intent);
					}
	    		};
        	description.setOnClickListener(MyListener);
        	
        	...
        	}
But this code doesn't work as it generates an error in Eclipse. I tried several ways of defining the "i" variable, but they all don't work. I also searched a lot but couldn't find an answer.

I hope one of you could help me out.

Ragoune

Edit: The error is the use of the "i" variable within the OnClickListener class.
 
Back
Top Bottom