Thank you for your help, it explained the problem and gave me the solution.

Mike


----- Original Message ----- 
From: "Odessa Silverberg" <[email protected]>
To: "Android Beginners" <[email protected]>
Sent: Wednesday, June 10, 2009 7:53 AM
Subject: [android-beginners] Re: OnKeyListener in a for loop



That's because you defined n inside a method.

change
  for (int n = 1; n < 7; n++)
in
  for (n = 1; n < 7; n++)

and add a
private int n = 0; in your Activities Class. It needs to be declared
so that it's accessible everywhere in the class, because you create
onKey(...) (which is an inline implementation of the OnKeyListener
Interface) can't access variables declared inside a method!!

On Jun 10, 1:50 pm, MMC2 <[email protected]> wrote:
> I am trying to enter 7 entries using an EditText in a for loop.
> But I get the error message "Cannot refer to a non final variable n
> inside an inner class defined in a different method. Without the for
> loop I can enter one entry. How do I set up a loop to do this.
>
> EditText enter_count = (EditText)findViewById(R.id.enter_count);
> for (int n = 1; n < 7; n++)
> {enter_count.setOnKeyListener(new OnKeyListener() {
> public boolean onKey(View v, int keyCode, KeyEvent
> event) {
> if (event.getAction() == KeyEvent.ACTION_DOWN) {
> if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
>
> TextView tv = (TextView)findViewById(R.id.mmc);
> EditText et = (EditText)findViewById
> (R.id.enter_count);
> tv.setText(denoms[n]+" x "+et.getText()+" = "+
> ((Double.parseDouble(et.getText().toString())) * 20));
> et.setText("");
> }
> }
> return false;}});
>
> }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to