Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:10:02+00:00 2026-05-28T22:10:02+00:00

So in Android/Java, I need to access a global variable that is an EditText[]

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:10:02+00:00Added an answer on May 28, 2026 at 10:10 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing one application in android for that I need to access com.android.internal.telephony
In my main activity I am having a method that I need to call
Does anyone know of any existing projects that aim to port Android's Java VM
I have to admit that I'm new to Java and Android. db4o seems to
I'm looking for a java library that works on the android that can download
It seems the java.net.NetworkInterface implementation of android does not have a byte[] getHardwareAddress() method
I have a C# application that will need to access files that are on
I have an Android java library that uses some native code. I have added
I need to create a global Arraylist consisting Instances of my custom class 'Book'
I'm building an Android application (as an Eclipse project) that needs to access a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.