1. Are you ready for the Galaxy S20? Here is everything we know so far!

Use variable in listener

Discussion in 'Android Apps & Games' started by Ragoune, Feb 23, 2011.

  1. Ragoune

    Ragoune Lurker
    Thread Starter

    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 (Text):
    1.  
    2. for (int i = 0; i < 3; i ++)
    3.             {
    4.             ...
    5.            
    6.             description = (TextView) findViewById(descriptions[i]);
    7.             description.setText(feeds[i].description);
    8.             OnClickListener MyListener = new OnClickListener()
    9.                 {
    10.                 @Override
    11.                 public void onClick(View v)
    12.                     {
    13.                     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(feeds[i].url));
    14.                     startActivity(intent);
    15.                     }
    16.                 };
    17.             description.setOnClickListener(MyListener);
    18.            
    19.             ...
    20.             }
    21.  
    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.
     

    Advertisement

  2. rhippler

    rhippler Well-Known Member

    you will probably get an answer over @ XDA forums. It is geared toward development.
     
Loading...
Similar Threads - variable listener
  1. marden
    Replies:
    3
    Views:
    189
  2. Guillermo Esquivel
    Replies:
    0
    Views:
    223
  3. emb16social
    Replies:
    1
    Views:
    336
  4. Brian Berry
    Replies:
    3
    Views:
    385
  5. fizznicus
    Replies:
    4
    Views:
    7,754
  6. ldm1417
    Replies:
    3
    Views:
    1,803
  7. Cofyka
    Replies:
    0
    Views:
    302
  8. RhinoCan
    Replies:
    6
    Views:
    760
  9. amankhalid
    Replies:
    5
    Views:
    1,626
  10. Brandomatic
    Replies:
    0
    Views:
    782

Share This Page

Loading...