1

So in Android/Java, I need to access a global variable that is an EditText[]that I create above the onCreate method of my activity inside of an onClickListener. Here is the variable:

public class NCDTCPRelayActivity extends Activity {
     final EditText[] relayLabelEdits = new EditText[n];

Where n is another global variable integer.

My problem is that in order to access these EditText inside an onclickListener i had to make final to access inside the onClickListener, but I need to assign relayLabelEdits to a new EditText[] if the value of n changes which it is wont to do.

Below is the onClickListener (the main portion of the app is to open a tcp socket and control relays via WiFi so just ignore the socket information and those System.out.println that point to the relayLabelEdits return null unless the relayLabelEdits variable is final):

    Button save = new Button(this);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText[] saveTargets = relayLabelEdits;
            System.out.println("Direct: " + relayLabelEdits[0]);
            System.out.println("first try " + saveTargets[0]);



            //Creates string to create the database names on the fly
            String relayToSave;

            int myNum = 0;
            try {
                myNum = Integer.parseInt(portEditText.getText().toString());
            } catch(NumberFormatException nfe) {
                Toast toast = Toast.makeText(getBaseContext(), "The Port Number you entered must only contain numbers", Toast.LENGTH_LONG);
                toast.show();
                return;
            }
            System.out.println(ipAEditText.getText().toString());
            cPanel.saveString("ipA", ipAEditText.getText().toString());
            cPanel.saveInt("port", myNum);

            for (int i = 0; i < n; i++) {
                relayToSave = "relay" + (i+1);
                System.out.println("HERE I AM");
                System.out.println(relayLabelEdits[i]);
                cPanel.saveString(relayToSave, relayLabelEdits[i].getText().toString());
                //cPanel.saveString(relayToSave, "It worked");
                System.out.println("HERE I AM");
            }

            if (cPanel.connect() == true){
                createMainContainer();
                setContentView(sView);
                getRelayNames();
                displayRelayNames();
                updateButtonText();
            }
            else {
                Toast toast = Toast.makeText(getBaseContext(), "Could Not Connect, check IP address and Port Number", Toast.LENGTH_LONG);
                toast.show();
            }
        }
    });

So any ideas on how I can access the EditText[] inside the onClickListener without making it final?

ethan
  • 780
  • 7
  • 20
Poodimizer
  • 590
  • 1
  • 6
  • 18
  • 1
    Just to clarify, there is no such thing as global variables in Java. `relayLabelEdits` is an ordinary class member with package scope. (That looks wrong to me. Why do you want to do that?) – mxk Feb 02 '12 at 17:46
  • So I can create an array of EditTexts in a for loop that will scale with the number of relays they have on the tcp server side. – Poodimizer Feb 02 '12 at 17:50
  • How would you go about doing it? – Poodimizer Feb 02 '12 at 19:05

1 Answers1

1

Why don't you make the listener a class outside and pass the activity reference to it?

Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • I'll have to look up how to do that, I've only used the nested ones. I'm fairly new to android so I haven't got a lot of the particulars down. I'll let you know how it goes. – Poodimizer Feb 02 '12 at 19:06
  • Ok that worked great. Can't believe I didn't think to try it. – Poodimizer Feb 02 '12 at 21:08